This question already has answers here:
python: changes to my copy variable affect the original variable [duplicate]
(4 answers)
Changing one list unexpectedly changes another, too [duplicate]
(5 answers)
Closed 4 years ago.
When I run the following block of code in python, it seems like for input x as a integer, func3 and func4 are correctly capturing the scope of the input. However, if x is an array, func2 sees input x not as [10], but as [9]. Why? Can functions in python change variables outside the scope of that specific function? Ie, it seems like func1 is modifying the global x.
def func1(x):
x[0] -= 1
return(x)
def func2(x):
x[0] -= 2
return(x)
def func3(x):
x -= 1
return(x)
def func4(x):
x -= 2
return(x)
if __name__ == "__main__":
x = [10]
print(func1(x)) # [9]
print(func2(x)) # [7]
x = 10
print(func3(x)) # 9
print(func4(x)) # 8
Related
This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Using global variables in a function
(25 answers)
Closed 2 years ago.
My code
x = 10
def fun():
x = x + 2
print(x)
fun()
print(x)
And the output error
UnboundLocalError: local variable 'x' referenced before assignment
You have to pass global x in the def fun() as no variable named x is assigned in fun(). Here is the code:
x = 10
def fun():
global x
x = x + 2
print(x)
fun()
print(x)
Output
>>> 12
>>> 12
OR
You can also simply pass an argument, but this makes a difference in the values of x:
x = 10
def fun(x):
x = x + 2
print(x)
fun(x)
print(x)
Output
>>> 12
>>> 10
Your variable x, that you're trying modify in the function fun(), is of global scope. Hence, you can't access it inside the function like that. However, you can use:
x = 10
def fun():
global x
x = x + 2
print(x)
fun()
print(x)
Appending global x, will allow the function to modify the variable x which is in global scope.
You don't give x as the parameter to the function. That's why.
x = 10
def fun(x):
x = x + 2
print(x)
fun()
print(x)
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
x = 10
def double(y):
return 2 * x
print(double(x))
Output is 20
As far as I know, it should return None because in function "double" I double x which is undefined that block.
x is defined outside of the function scope as a global variable that is also available in the function
It is because x is a global variable, so when you call double it is just multiplying x by 2 no matter what
if you were to say put
x = 10
def double(y):
return 2 * x
print(double(40))
you would still get 20 as you are returning the variable x multiplied by 2.
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 2 years ago.
I want function(x) to change the value of whatever global variable is put into it, say add one to the variable, like so:
>>> a = 1
>>> function(a)
>>> print(a)
2
I tried:
def function(x):
global x
x = x + 1
However, this returns a SyntaxError name 'x' is parameter and global...
a=1 #global variable
def function():
global a
a=a+1 #operation on variable a
return a
function()
is this answer your query?
This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Pass list to function by value [duplicate]
(4 answers)
Closed 6 years ago.
For instance,
def T(x):
for i in range(1,len(x)-1):
x[i]+=x[i-1]+2
def f(x):
x=x+2
return x
x=[1,2,3,4,5]
;T(x)
;print(x)
[1, 5, 10, 16, 5]
the variable x changes in this case but,
x=3
;f(x)
;print(x)
x=3
x does not change in this case.
why is this happening?
Generally speaking, mutable object is passed as reference while immutable ones are passed by values.
To get same result as (1):
x = 3
x = f(x)
print(x)
You can check this web for more info on this.
This question already has answers here:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 6 years ago.
So here is the code that uses x inside a function.
x = 1
def f():
y = x
x = 2
return x + y
print x
print f()
print x
but python is not going to look up the variable out of function scope , and it results in UnboundLocalError: local variable 'x' referenced before assignment . I am not trying to modify the value of global variable , i just want to use it when i do y=x.
On the other hand if i just use it in return statment , it works as expected:
x = 1
def f():
return x
print x
print f()
Can some one explain it why?
you have to specify global x in your function if you want to modify your value,
however it's not mandatory for just reading the value:
x = 1
def f():
global x
y = x
x = 2
return x + y
print x
print f()
print x
outputs
1
3
2