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
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:
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:
UnboundLocalError trying to use a variable (supposed to be global) that is (re)assigned (even after first use)
(14 answers)
Closed 4 years ago.
x = 5
def foobar():
print (x) #Prints global value of x, which is 5
x = 1 #assigns local variable x to 1
foobar()
Instead, it throws a
UnboundLocalError: local variable 'x' referenced before assignment
What am I misunderstanding in the comments? Note, I understand if i do x=x+1, it'll throw error due to 'accessing value of local scope x beforei it's defined', but in this case I'm doing x=1, which does not require reading of existing value of x! This is NOT a duplicate question.
If you want to change a global variables value in a function in python, you have to do
x = 5
def foobar():
global x
print (x) #Prints global value of x, which is 5
x = 1 #assigns local variable x to 1
foobar()
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 5 years ago.
x=100
def fun2():
print x
x=10000
print x
fun2()
The above program showing local variable x reference before assignment. Why it is not printing
100
10000
x in the function is a local variable and can't access the other local variable you define first because they are in different scope.
Add global x to the start of your function or define x inside the function.
You appear to not know about variable scoping.
The variable x does not exist in the function scope.
You have to place global x before your print statement in order to access the global variable x.
x = 1 # Global x
def f():
x = 2 # function-local x
print(x) # prints 2
f()
print(x) # prints 1 because it uses the global x which remains unchanged
If you want that to work you need to specify inside the function that the x variable you are using is the one in the global scope by using the global keyword.
x=100
def fun2():
# Add this line
global x
print x
x=10000
print x
fun2()
Below code will print the value of x -> 100, as it is there in main scope #samba, but when you change the value of it doesn't work that way as it is not defined in the function.
x = 100
def fun2():
print(x)
fun2()
This doesn't work as the same way:
x = 100
def fun2():
print(x)
x = 1000
print(x)
fun2()
and through error:
UnboundLocalError: local variable 'x' referenced before assignment
x is a local variable and not initialised in function fun2().
You need to understand variable scoping here, Please check Global and Local variable scope
If you want to use it globally use global keyword in your function.
Because u assigned variable before function.
Just try this
def fun2():
x=100
print x
x=10000
print x
fun2()
It will output 100 and 1000
This question already has answers here:
Calling variable defined inside one function from another function
(6 answers)
Python: Variables aren't re-assigned
(2 answers)
Closed 7 years ago.
I tried the following and got an error message that "x is not defined".
def test():
x = 2
y = 3
return x
def main():
test()
print(2 + x)
main()
Why isn't it working?
x is defined in test, but not in main
This means that x is not defined in main's scope
Take a look at this question for more details regarding scope. Specifically this answer.
If you want to use the value assigned to x in test, you need to assign the return value of test to a name:
def main():
value_of_x_from_test = test() # This is the same as `x = 2` in `test`.
print(2 + value_of_x_from_test)
You need to assign x again, and the way to do that is say x = test(). The x from test only exists in x, which is why you need to reassign it.
It doesn't even have to be x, it could be y, or z: x is simply the name you return, the value you use in main() can be called anything!
Or, you can just call the function directly in main(), in which case you won't even need the first line in the main function:
def test():
x = 2
y = 3
return x
def main():
x = test()
print(2 + x) #or just `print(2 + test())`
main()
It's not working because of scope. X is defined in test, but not in main(). Try setting x = test() in main, and everything should work for you.
In order to get the value of x in main(), you have to assign it to something:
def main():
value = test() # The variable can be 'value' or any other valid name, including 'x'
print(2 + value)
Because of x is defined locally in test(), it cannot be used by main. Instead you can use directly the output of test as it is shown in the following code:
def main():
print(2 + test())