Python:UnboundLocalError帮助:分配前引用了局部变量 [英] Python: Help with UnboundLocalError: local variable referenced before assignment

查看:368
本文介绍了Python:UnboundLocalError帮助:分配前引用了局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部分代码不断收到此错误.

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

我没有编写代码,因为它在函数之间来回移动.我想知道是否有人可以告诉我为什么python会吐出这个错误? sshfile不是变量,而是一个类.

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? 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. It depends on how you imported it.

它来自什么包装?在哪里定义?

What package does it come from? Where is it defined?

更新

对于其他阅读此内容的人,在评论中进行讨论后,发现问题在于名称sshfile已在函数中进一步用作变量名,如下所示:

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:

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.

这篇关于Python:UnboundLocalError帮助:分配前引用了局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆