0

When you state that a variable is global, it does not create it for you (if it doesn't already exist). What does the global statement actually do to the variable? It obviously doesn't merely modify it since it does not have to exist for it to be modified. Once this goes out of scope, can

def foo():
    global cat, dog
    dog = 1

foo()
print('dog' in globals())  # => True
print(dog)  # => 1
print('cat' in globals())  # => False
print(cat)  # => NameError

This also raises an error (not surprising):

def foo():
    global cat, dog
    dog = 1

def bar():
    cat = 2

foo()
bar()
print(dog)
print(cat)  # => NameError

So obviously the global modifier only works within the scope of the function being executed. Is this, in any way, caused by the garbage collector? Is there some phantom globalizer object that waits for the creation of an object with the given name and gets cleared up upon the end of the function?

8
  • Right but prior to assigning dog to a value, there were no instances of dog. Commented Jun 7, 2016 at 22:45
  • My point is that it's obviously not directly modifying dog because dog does not exist prior to the global statement. You cannot modify something that doesn't exist. Commented Jun 7, 2016 at 22:46
  • The garbage collector has nothing to do with it. global just doesn't create the variable. If you check "cat" in globals() inside the foo(), it will answer false. Commented Jun 7, 2016 at 22:48
  • You assign dog to a value in foo so I am not sure what you mean. If you were to print cat in foo you would get a NameError as it is not defined anywhere. Commented Jun 7, 2016 at 22:50
  • 1
    docs.python.org/2/reference/… global is a directive to the parser. It applies only to code parsed at the same time as the global statement. Commented Jun 7, 2016 at 22:52

2 Answers 2

2

What does the global statement actually do to the variable?

Absolutely nothing.

global foo means that any occurrences of the variable name foo in the scope of the function refer to a module-global foo variable instead of a function-call-local variable. It does nothing to the variable itself.

As for where such variables live, they don't really "live" anywhere. When such a variable is assigned, an entry will be created for them in the module's global variable dict. If the variable is deleted, the global variable dict entry will be erased. This is identical to what would happen if you were assigning and deleting these variables at module level without a global declaration.

Sign up to request clarification or add additional context in comments.

Comments

1

The global is a directive to the parser.

as written in the doc. This means it doesn't change anything. Also notice that "it applies only to code parsed at the same time as the global statement". This can be tested in the below example

a=3
def foo():
  exec('global a')
  a=4

foo()
print(a) # 3

If global is a modifier as you said, then the last line will print 4. But that's not the case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.