The global statement in python


寫 code 時候踩到地雷,果然沒唸好 scope 果然是不行的

#!/usr/bin/env python

flag = True

def test():
    flag = False

print flag
test()
print flag

結果似乎和想的不太一樣。


# python 2.py
True
True

結果翻了一下 The global statement 才知道

It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

,

One response to “The global statement in python”

Leave a Reply

Your email address will not be published. Required fields are marked *