views:

210

answers:

2

What is the difference between initializing a variable as global var or calling globals().update(var).

Thanks

+7  A: 

When you say

global var

you are telling Python that var is the same var that was defined in a global context. You would use it in the following way:

var=0
def f():
    global var
    var=1
f()
print(var)
# 1  <---- the var outside the "def f" block is affected by calling f()

Without the global statement, the var inside the "def f" block would be a local variable, and setting its value would have no effect on the var outside the "def f" block.

var=0
def f():
    var=1
f()
print(var)
# 0  <---- the var outside the "def f" block is unaffected

When you say globals.update(var) I am guessing you actually mean globals().update(var). Let's break it apart.

globals() returns a dict object. The dict's keys are the names of objects, and the dict's values are the associated object's values.

Every dict has a method called "update". So globals().update() is a call to this method. The update method expects at least one argument, and that argument is expected to be a dict. If you tell Python

globals().update(var)

then var had better be a dict, and you are telling Python to update the globals() dict with the contents of the var dict.

For example:

#!/usr/bin/env python

# Here is the original globals() dict
print(globals())
# {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__file__': '/home/unutbu/pybin/test.py', '__doc__': None}

var={'x':'Howdy'}
globals().update(var)

# Now the globals() dict contains both var and 'x'
print(globals())
# {'var': {'x': 'Howdy'}, 'x': 'Howdy', '__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__file__': '/home/unutbu/pybin/test.py', '__doc__': None}

# Lo and behold, you've defined x without saying x='Howdy' !
print(x)
Howdy
unutbu
Ok so, if I had a variable declared in a method, and in that method called globals().update('var':'0') only during the first run of the script, I wouldn't be able to use 'var' in the same way as a variable that has been declared as "global var", throughout the rest of the code upon the second execution of the script?
frank
globals().update('var':'0') should be globals().update({'var':'0'}). This will affect the global variable 'var', even if this command was put inside a method. The behavior of these commands is the same on every run of the script. If this does not answer your question, please post your code so we can get a better understanding.
unutbu
Ok, it's short but the formatting will be weird:try: i+=1except NameError: i=0. That code will be in a main method, and I can't change it, or add any method declarations before the code / outside of the method. What I can do is add code in the main method after this little code snippet. I need to make 'i' act as a global variable, so that it will increment by '1' on every successive script execution.
frank
@frank: Please ask this "What I can do is add code in the main method after this little code snippet?" question as a separate question with enough details that it is a stand-alone question and reasonably clear.
S.Lott
+1  A: 

globals().update(foo) and global foo are not equivalent. If you want to reference global variable directly, then use globals()['foo']. Difference - global foo creates a local reference to global variable, while globals() provides the direct access.

PiotrLegnica