本文介绍了Python的全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何定义全局变量.
例如,我有Foo1类和Foo2类,并且我想在此类中使用FooVariable作为全局变量.
For example I have Foo1 class and Foo2 class and I want to use FooVariable in this classes as a global variable.
推荐答案
如果您想要一个名为 foo
的全局变量,则只需将 global foo
放在使用它的任何功能的顶部.例如,
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
请注意,Python全局"变量仅对它们所出现的模块是全局的,而不是整个程序的全局(如某些语言).
Note that Python "global" variables are only global to the module they appear in, not global to the entire program (as in some languages).
这篇关于Python的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!