可能重複:
Using global variables in a function other than the one that created them蟒蛇全局變量
我如何定義一個全局變量。
例如我有Foo1類和Foo2類,我想在這個類中使用FooVariable作爲全局變量。
可能重複:
Using global variables in a function other than the one that created them蟒蛇全局變量
我如何定義一個全局變量。
例如我有Foo1類和Foo2類,我想在這個類中使用FooVariable作爲全局變量。
如果你想要一個名爲foo
一個全局變量,你只要把global foo
在其使用的任何功能的頂部。例如,
def do_something():
global foo
foo = foo + 1
print foo
需要注意的是Python的「全局」變量是全球唯一的,以它們出現在,沒有全局的整個程序模塊(如一些語言)。