簡體   English   中英

Python:UnboundLocalError幫助:分配前引用了局部變量

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

我的部分代碼不斷收到此錯誤。

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 ,或者您需要使用包名來限定類名。 這取決於您如何導入。

它來自什么包裝? 它在哪里定義?


更新

對於任何其他閱讀此內容的人,在評論中進行討論后,發現問題在於名稱sshfile已在函數中進一步用作變量名,如下所示:

class sshfile:
    pass

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

a()

解決方案是不要使用隱藏您需要使用的類名的變量名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM