Python module globals versus __init__ globals
Question
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
.......
globvar = 1
class dog:
def bark(self):
print globvar
class cat:
def miaow(self):
print globvar
What is the difference between this and
class dog:
def __init__(self):
global globvar
def bark(self):
print globvar
class cat:
def miaow(self):
print globvar
Assuming I always instantiate a dog first?
I guess my question is, is there any difference? In the second example, does initiating the dog
create a module level globvar
just like in the first example, that will behave the same and have the same scope?
Solution
global
doesn't create a new variable, it just states that this name should refer to a global variable instead of a local one. Usually assignments to variables in a function/class/... refer to local variables. For example take a function like this:
def increment(n)
# this creates a new local m
m = n+1
return m
Here a new local variable m
is created, even if there might be a global variable m
already existing. This is what you usually want since some function call shouldn't unexpectedly modify variables in the surrounding scopes. If you indeed want to modify a global variable and not create a new local one, you can use the global
keyword:
def increment(n)
global increment_calls
increment_calls += 1
return n+1
In your case global
in the constructor doesn't create any variables, further attempts to access globvar
fail:
>>> import animals
>>> d = animals.dog()
>>> d.bark()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "animals.py", line 7, in bark
print globvar
NameError: global name 'globvar' is not defined
But if you would actually assign a value to globvar
in the constructor, a module-global variable would be created when you create a dog:
class dog:
def __init__(self):
global globvar
globvar = 1
...
Execution:
>>> import animals
>>> d = animals.dog()
>>> d.bark()
1
>>> print animals.globvar
1
OTHER TIPS
No, the global
statement only matters when you're assigning to a global variable within a method or function. So that __init__
is irrelevant -- it does not create the global, because it's not assigning anything to it.