Pythons global variable [duplicate]
How can I define a global variable.
For example I have Foo1 class and Foo2 class and I want to use FooVariable in this classes as a global variable.
If you wanted a global variable named foo
, you would just have to put global foo
at the top of any function in which it is used. For example,
def do_something():
global foo
foo = foo + 1
print foo
Note that Python "global" variables are only global to the module they appear in, not global to the entire program (as in some languages).
精彩评论