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

I have a terrible problem trying to understand python review 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() 

It gives an unexpected (for me) conclusion:

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

Where would I expect the last print of a to be 42, not 7. What am I missing from the rules of the Python scope for defining global variables?

+48
scope python global-variables
May 30 '09 at 14:09
source share
4 answers

Global variables are special. If you try to assign the variable a = value inside the function, it will create a new local variable inside this function, even if there is a global variable with the same name. To access the global variable instead, 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 naming and binding rules.

+96
May 30 '09 at 14:12
source share

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

This becomes more obvious if you try to print the value of a at the beginning of setA (value) as follows:

 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, you will get a useful 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 decided that the setA (value) function has a local variable called a, which you change when you assign it a function. If you do not assign a to a 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 area in which you want to use the global variable. In this case, it is the setA (value) function. Thus, 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 a variable in the setA (value) function, you are talking about a global variable, not a local variable.

+10
May 30 '09 at 14:17
source share

Python has no concept of variables like other languages. You have objects that are β€œsomewhere,” and you have links to these objects. = is used to assign these objects to links in the current namespace.

You create the name a in the namespace of the setA function, which refers to the object to which the value refers.

+1
May 30 '09 at
source share

inside a function, a is treated as a local variable, you need to define

global a

inside function

+1
Jan 16
source share



All Articles