我定义了一个函数,在这个函数或另一个函数中,我通过调用同名函数来分配相同的名称值。我得到这个错误
UnboundLocalError: local variable 'demo01' referenced before assignment
错误发生在这里
def demo01():
return 5
def demo02():
demo01=demo01()
demo02()
UnboundLocalError: local variable 'demo01' referenced before assignment
但这些片段很好
^{2}$def demo01():
return 5
def demo02():
demo02=demo01()
demo02()
当存在现有变量时,在该名称下创建新变量将覆盖现有变量,例如:
将给予
^{pr2}$回到你的代码:
显然不可能;给予
在全局状态下使用时,在本地(类或函数)中使用时为UnboundLocalError。在
赋值过程中使用的变量名和其他变量(如果有)不得是或引用当前分配的变量。在
如果确实需要使用相同的变量:
相关问题 更多 >
编程相关推荐