You need to actually call function1()
first.
The global
keyword only tells Python that when you call the function, any assignment to var1
should be to a global, not a local name. But that doesn't magically make var1
appear until you actually execute that assignment. Until you call function1
, there is no global var1
.
Python has no declarations; names are either bound or not bound.
Demo:
>>> def function1():
... global var1
... var1 = 'Hello'
...
>>> def function2():
... return var1
...
>>> function2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in function2
NameError: global name 'var1' is not defined
>>> function1()
>>> function2()
'Hello'