Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does assigning to my global variables not work in Python?

I'm having terrible trouble trying to understand python scoping rules.

With the following script:

a = 7  def printA():     print "Value of a is %d" % (a)  def setA(value):     a = value     print "Inside setA, a is now %d" %(a)   print "Before setA" printA() setA(42) print "After setA" printA() 

Gives the unexpected (to me) output of:

     Before setA     Value of a is 7     Inside setA, a is now 42     After setA     Value of a is 7 

Where I would expect the last printing of the value of a to be 42, not 7. What am I missing about Python's scope rules for the scoping of global variables?

like image 784
Free Wildebeest Avatar asked May 30 '09 14:05

Free Wildebeest


People also ask

How do you're assign a global variable in Python?

Use of “global†keyword to modify global variable inside a function. If your function has a local variable with same name as global variable and you want to modify the global variable inside function then use 'global' keyword before the variable name at start of function i.e.

Why is global variable not defined Python?

Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only.

Why variables are not declared in Python?

Python has no command for declaring a variable. A variable is created when some value is assigned to it. The value assigned to a variable determines the data type of that variable. Thus, declaring a variable in Python is very simple.

Why is my global variable undefined?

The reason the first alert is undefined is because you re-declared global as a local variable below it in the function. And in javascript that means from the top of the function it is considered the local variable.


2 Answers

Global variables are special. If you try to assign to a variable a = value inside of a function, it creates a new local variable inside the function, even if there is a global variable with the same name. To instead access the global variable, add a global statement inside the function:

a = 7 def setA(value):     global a   # declare a to be a global     a = value  # this sets the global value of a 

See also Naming and binding for a detailed explanation of Python's naming and binding rules.

like image 142
Adam Rosenfield Avatar answered Oct 14 '22 19:10

Adam Rosenfield


The trick to understanding this is that when you assign to a variable, using =, you also declare it as a local variable. So instead of changing the value of the global variable a, setA(value) actually sets a local variable (which happens to be called a) to the value passed in.

This becomes more obvious if you try to print the value of a at the start of setA(value) like so:

def setA(value):     print "Before assignment, a is %d" % (a)     a = value     print "Inside setA, a is now %d" % (a) 

If you try to run this Python will give you a helpful error:

 Traceback (most recent call last):   File "scopeTest.py", line 14, in      setA(42)   File "scopeTest.py", line 7, in setA     print "Before assignment, a is %d" % (a) UnboundLocalError: local variable 'a' referenced before assignment 

This tells us that Python has decided that the setA(value) function has a local variable called a, which is what you alter when you assign to it in the function. If you don't assign to a in the function (as with printA()) then Python uses the global variable A.

To mark a variable as global you need to use the global keyword in Python, in the scope that you want to use the global variable. In this case that is within the setA(value) function. So the script becomes:

a = 7  def printA():     print "Value of a is %d" % (a)  def setA(value):     global a     a = value     print "Inside setA, a is now %d" %(a)   print "Before setA" printA() setA(42) print "After setA" printA() 

This one line addition tells Python that when you use the variable a in the setA(value) function that you are talking about the global variable, not a local variable.

like image 36
Free Wildebeest Avatar answered Oct 14 '22 21:10

Free Wildebeest