欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python | local variable 'xxxx' referenced before assignment

程序员文章站 2022-08-03 18:43:15
函数func_in的局部变量num未定义而引发的错误: local variable 'num' referenced before assignment 函数func_in即使定义在函数func中,但它的变量仍然是局部变量,与函数func的参数num不相关。 ......
>>> 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不相关。