这是一种奇怪的行为.
试试这个 :
rep_i=0
print "rep_i is" , rep_i
def test():
global rep_i #without Global this gives error but list , dict , and others dont
if rep_i==0:
print "Testing Integer %s" % rep_i
rep_i=1
return "Done"
rep_lst=[1,2,3]
def test2():
if rep_lst[0]==1:
print "Testing List %s" % rep_lst
return "Done"
if __name__=="__main__":
test()
test2()
Run Code Online (Sandbox Code Playgroud)
为什么列表不需要声明全局?他们是自动全球的?
我发现它真的很奇怪,我大部分时间都使用列表,我甚至根本不使用全球它们作为全球.....