简体   繁体   English

Python:UnboundLocalError帮助:分配前引用了局部变量

[英]Python: Help with UnboundLocalError: local variable referenced before assignment

I keep getting this error for a portion of my code. 我的部分代码不断收到此错误。

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

I haven't put the code up cause it goes back and forth between functions. 我没有编写代码,因为它在函数之间来回移动。 I'm wondering if anyone could tell me why python is spitting this error? 我想知道是否有人可以告诉我为什么python会吐出这个错误? sshfile is not a variable it's a class. sshfile不是变量,而是一个类。

You probably haven't imported the file which contains the definition of sshfile , or you need to qualify the class name with the package name. 您可能尚未导入包含sshfile定义的sshfile ,或者您需要使用包名来限定类名。 It depends on how you imported it. 这取决于您如何导入。

What package does it come from? 它来自什么包装? Where is it defined? 它在哪里定义?


Update 更新

For anyone else reading this, after a discussion in the comments it turned out that the problem was that the name sshfile had been used further down in the function as a variable name, like this: 对于任何其他阅读此内容的人,在评论中进行讨论后,发现问题在于名称sshfile已在函数中进一步用作变量名,如下所示:

class sshfile:
    pass

def a():
    f = sshfile() # UnboundLocalError here
    sshfile = 0

a()

The solution is to not use a variable name that hides a class name that you need to use. 解决方案是不要使用隐藏您需要使用的类名的变量名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 "帮助 Python UnboundLocalError:赋值前引用的局部变量" - Help with Python UnboundLocalError: local variable referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) python - UnboundLocalError:分配前引用的局部变量 - python - UnboundLocalError: local variable referenced before assignment Python-UnboundLocalError:分配前引用的局部变量 - Python - UnboundLocalError: local variable referenced before assignment` Python UnboundLocalError:分配前引用了局部变量 - Python UnboundLocalError: local variable referenced before assignment Python | 如果变量:| UnboundLocalError:赋值前引用的局部变量&#39;variable&#39; - Python | if variable: | UnboundLocalError: local variable 'variable' referenced before assignment UnboundLocalError:在python闭包中赋值之前引用的局部变量 - UnboundLocalError: local variable referenced before assignment in python closure Python(3.3):UnboundLocalError:分配前已引用局部变量 - Python (3.3): UnboundLocalError: local variable referenced before assignment Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment UnboundLocalError:分配前引用的局部变量“转” - python - UnboundLocalError: local variable 'turn' referenced before assignment - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM