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
精彩评论