Why is the output not 1 and 1? [duplicate] - python

This question already has an answer here:
How do the scoping rules work with classes?
(1 answer)
Closed 2 years ago.
a = 0
b = 0
def test():
a = 1
b = 1
class Test:
print(a, b)
a = 2
test()
It gives
0 1
It should be
1 1
Why is this happening?

Inside the test function, you're not actually initializing a new Test object, so Python will read through the class definition to use it inside the function scope, so it does execute the print statement. The problem is that you are trying to change the value of a in the class scope and this generates that a take the previous value of a outside the function definition.

Related

My variables updated in a function don't work outside the function [duplicate]

This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Function not changing global variable
(4 answers)
Closed 1 year ago.
hi = 1
result = 0
def hello(x):
x = x + 10
result = x
helli(hi)
print(result)
Why does my code Output "0" and not 11?
You need to use the keyword global for that. Then edited values inside the function actually impact the variable from outside the function. This is not the recommended approach.
hi = 1
result = 0
def hello(x):
global result
x = x + 10
result = x
hello(hi)
print(result)
The recommended approach is, however, to return such value:
def hello(x):
return x + 10
hi = 1
result = 0
result = hello(hi)
print(result)

Call a variable outside of a function? [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 3 years ago.
For example:
def test(a)
a = 2
b = 5+a
x = a*2
return x
Is there a way to call the variable 'b' outside of the function?
b = None
def test(a)
global b
a = 2
b = 5+a
x = a*2
return x
print(b)
Although we will suggest to return b and then access it, as global variable is a bad idea.
See this,
Why are global variables evil?

Is there a bug in numpy when using the += operator [duplicate]

This question already has answers here:
What is the difference between i = i + 1 and i += 1 in a 'for' loop? [duplicate]
(6 answers)
When is "i += x" different from "i = i + x" in Python?
(3 answers)
Understanding Python's call-by-object style of passing function arguments [duplicate]
(3 answers)
How do I pass a variable by reference?
(39 answers)
Closed 3 years ago.
When using a function in phyton, I am taught that actually a copy of my value is parsed. In the example shown below apply a function onto a Parameter a. Here, I expected that a copy of a is sent to the function fun. In this function, only the copy of a is available, not parameter a on a global scope. I even gave it another Name: b. When I modify the value b in my function, then also the Parameter a on the global scope is changed. Is this supposed to be correct?
import numpy as np
def fun(b):
b += np.array([1,1])
a = np.array([1,1])
fun(a)
print(a)
I expected to get np.array([1,1]), but I get np.array([2,2])
This happens only, when I use the += Operator in the function fun. If I use b = b + np.array([1,1]) instead, the value of a on global scope stays the same.
When you call fun() you don't make a copy of a and modify b, rather, a itself is passed to fun() and refered to as b in there.
As such,
b += np.array([1,1])
modifies a.
In python the list datatype is mutable, that means that every time you run your function, a list will keep growing.
The key moment is here: b += np.array([1,1])
You already got a, which is [1, 1] and you adding it to another array which is [1, 1] and you are getting [2, 2] which is how it should be.
You are ending up modifying the a

Using a variable outside of a defined function [duplicate]

This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 months ago.
If I had a function that I made:
def a():
n = 2*2
How could I access n out of the function without calling a?
You cannot. You will need to define the variable outside of the function, or call the function and return it.
You need to return it, so do:
def a():
n = 2*2
return n
print(a())
Output:
4
You can also do print, but return is better, check this: What is the formal difference between "print" and "return"?

How should I code a function to turn a parameter I input into 1? [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 8 years ago.
I am the new to Python. Here is a problem that I have encountered.
Assume that there is a function called set1(x).
Here is the code:
def set1(x):
x = 1;
When I run this,
m = 5
set1(m)
m
the value of m is still 5.
So how should I code the function if I want the parameter to become 1 whenever I call the set1() function?
You can return the value
def set1():
return 1;
And call it as
m=5
m = set1()
print (m)
It will print 1
Another way but bad method is to make it global
def set1():
global m
m = 1
Functions use a local namespace in which they declare their variables, which means that in most cases when you pass an argument to a function, it will not effect the argument outside of the function (in the global namespace). So while the value changes within the function, it does not change outside of the function.
If you want the function to change the value, you need to return the value as such:
def set1(x):
x=1
return x
>>> m=5
>>> m = set1(m)
>>> m
1
This said, if your argument is mutable, it can change.
def set2(x):
x[0] = 1
>>> m = [2]
>>> set2(m)
>>> m[0]
1

Categories

Resources