开发者

How do I set a top level variable in Python?

开发者 https://www.devze.com 2023-02-04 10:49 出处:网络
Let\'s say I have a script file, like so: foo = 1 def bar(): return foo # The Main function for this script.

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

精彩评论

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

关注公众号