Python | local variable 'xxxx' referenced before assignment

>>> def func(num):
...     def func_in():
...             num += 1
...             print(num)
...     return func_in
... 
>>> fun = func(10)
>>> fun
<function func.<locals>.func_in at 0x1034410d0>
>>> fun()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in func_in
UnboundLocalError: local variable 'num' referenced before assignment

 函数func_in的局部变量num未定义而引发的错误: local variable 'num' referenced before assignment

 函数func_in即使定义在函数func中,但它的变量仍然是局部变量,与函数func的参数num不相关。

猜你喜欢

转载自www.cnblogs.com/qiutenglong/p/11272323.html