开发者

Pythons global variable [duplicate]

This question already has answers here: Using global variables in a function (25 answers) 开发者_如何学Go Closed 4 years ago.

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜