Python函数内部定义之前调用报错求助,python函数,def str(s):pdef str(s):print 'global str()'def foo(): str('dummy') def str(s): print 'closure str()' str('dummy')def bar(): str('dummy') print 'call'foo()bar()
现象: 在foo()函数里面 第一次调用str的时候 提示:UnboundLocalError: local variable 'str' referenced before assignment
问题:
1. 在运行foo()的时候,第一次调用str, foo本地命名空间没有str的定义,这时候要去外部找str, 为什么会找不到外部定义的str
2. foo内部有str的定义, 解释器第一次读入foo定义的时候, 内部的str要怎么处理
3. 为什么会报这个错误, 类似的bar(), 为什么能够找到外部的str
谢谢~~
https://docs.python.org/2/reference/executionmodel.html
编橙之家文章,