如果代码风格相对而言不是那么的pythonic,或许很少碰到这类错误。当然并不是不鼓励使用一些python语言的技巧。如果遇到这这种类型的错误,说明我们对python中变量引用相关部分有不当的认识和理解。而这又是对理解python相关概念比较重要的。这也是本文写作的原因。
本文为理解闭包相关概念的做铺垫,后续会详细深入的整理出闭包相关的博文,敬请关注。
1.案例分析
在整理闭包相关概念的过程中,经常发现UnboundLocalError和NameError这两个错误,刚开始遇到的时候可能很困惑,对这样的错误无从下手。
1.1 案例一:
def outer_func(): loc_var = "local variable" def inner_func(): loc_var += " in inner func" return loc_var return inner_funcclo_func = outer_func()clo_func()
错误提示:
Traceback (most recent call last):
File "G:/Project Files/Python Test/Main.py", line 238, in <module>
clo_func()
File "G:/Project Files/Python Test/Main.py", line 233, in inner_func
loc_var += " in inner func"
UnboundLocalError: local variable 'loc_var' referenced before assignment
1.2 案例二:
def get_select_desc(name, flag, is_format = True): if flag: sel_res = 'Do select name = %s' % name return sel_res if is_format else name get_select_desc('Error', False, True)
错误提示:
Traceback (most recent call last):
File "G:/Project Files/Python Test/Main.py", line 247, in <module>
get_select_desc('Error', False, True)
File "G:/Project Files/Python Test/Main.py", line 245, in get_select_desc
return sel_res if is_format else name
UnboundLocalError: local variable 'sel_res' referenced before assignment
1.3 案例三:
def outer_func(out_flag): if out_flag: loc_var1 = 'local variable with flag' else: loc_var2 = 'local variable without flag' def inner_func(in_flag): return loc_var1 if in_flag else loc_var2 return inner_funcclo_func = outer_func(True)print clo_func(False)
错误提示:
Traceback (most recent call last):
File "G:/Project Files/Python Test/Main.py", line 260, in <module>
print clo_func(False)
File "G:/Project Files/Python Test/Main.py", line 256, in inner_func
return loc_var1 if in_flag else loc_var2
NameError: free variable 'loc_var2' referenced before assignment in enclosing scope
上面的三个例子可能显得有点矫揉造作,但是实际上类似错误的代码都或多或少可以在上面的例子中找到影子。这里仅仅为了说明相关概念,对例子本身的合理性不必做过多的关注。
新闻热点
疑难解答