开发者

How do I set a top level variable in Python?

Let's say I have a script file, like so:

foo = 1

def bar():
    return foo

# The Main function for this script.
def main(a开发者_Go百科rgv=None):
    foo = 2
    print bar()
    return

This prints 1. Is there any way to set the value of foo at the script-level from main?


foo = 1

def bar():
    return foo

# The Main function for this script.
def main(argv=None):
    global foo
    foo = 2
    print bar()
    return

This is in the python documentation.


def main(argv=None):
    global foo
    foo = 2
    print bar()
    return
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜