- Python code
>>> def func(): t.start() # 为什么在函数内部可以使用定义在函数外面的对象t? for i in range(5): print(i) >>> t = Mythread(2) <-- t是在函数外面定义的 >>> func() 0 1 2 3 4 >>> 2
------解决方案--------------------
python变量作用域遵循一个称为LGB的规则,
This is the so-called LGB rule of name resolution: local, then global, then built-in.
请参考:
http://www.magicalboy.com/python-scope-legb.html
http://www.manaware.net/language-processing/variable-scope.html