我的部分代码不断收到此错误。
Traceback (most recent call last):
File "./mang.py", line 1688, in <module>
files, tsize = logger()
File "./mang.py", line 1466, in logger
nl = sshfile(list, "nl")
UnboundLocalError: local variable 'sshfile' referenced before assignment
我没有把代码放上去,因为它在函数之间来回切换。我想知道是否有人能告诉我为什么 python 会吐出这个错误? sshfile 不是一个变量,它是一个类。
最佳答案
您可能还没有导入包含 sshfile
定义的文件,或者您需要用包名来限定类名。这取决于您如何导入它。
它来自什么包?在哪里定义的?
更新
对于阅读本文的任何其他人,在评论中进行讨论后发现问题是名称 sshfile
已在函数中进一步用作变量名,如下所示:
class sshfile:
pass
def a():
f = sshfile() # UnboundLocalError here
sshfile = 0
a()
解决方案是不要使用隐藏您需要使用的类名的变量名。
关于Python:帮助解决UnboundLocalError:赋值前引用的局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2256056/