有朋友提到如下两个程序的表现不一,于是我提交了一个bug(http://bugs.python.org/issue1888)。很快得到了GvR的答复,很显然,他的答复只是从一个python开发者的角度进行的;完全忽略python语法的一致性、考验大家对英语的理解(下面的yield我可以理解啊)。不过,也只好记着小心使用了,谁叫咱不会开发自己的语言呢。
#a.py
def run():
if 1==2: # note, it always False
global a
a=1
run()
a
#b.py
def run():
a=1
run()
a
GvR的答复
This isn't going to change. 'global' is one of those rare pieces of
syntax that are not executed by the interpreter but detected by the parser.
Note that there are other things like this, e.g. the presence of 'yield'
in a function makes it a generator.
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=6610343