簡體   English   中英

調用 function 時出現“UnboundLocalError:賦值前引用的局部變量”

[英]"UnboundLocalError: local variable referenced before assignment" when calling a function

對於繪圖,我根據條件定義顏色(條件是 pandas 數據幀的某些列中的一些值)。 現在我不確定,如果我在定義 function 時犯了錯誤。 function如下:

def getColour(C, threshold):
    neg = 0 - threshold
    half = threshold/2
    if C <= (neg - half):
        clr = '#2b83ba'
    if ((neg - half) < C <= neg):
        clr = '#abdda4'
    if ((threshold + half) > C >= threshold):
        clr = '#fdae61'
    if (C > (threshold + half)):    
        clr = '#d7191c'
    return clr

這就是我實現它的方式:我遍歷 dataframe 的行,然后找到滿足條件的列,使用這些列中的索引從列表中獲取參數,應用另一個生成結果的 function,此 ZC1C125268E68A935當我為繪圖設置固定顏色時經過測試並且工作正常)然后 plot 使用不同顏色的結果。

for index, row in Sparse.iterrows():
    lim = row[row.notnull()] 
    ci = [row.index.get_loc(x) for x in lim.index]
    params = np.array(myList)[ci]

    for i, w in enumerate(params):
        w = w.tolist()
        print w, w[2]
        print ci[i]
        colour = getColour(ci[i], threshold)
        x, y = myFunction(w)
        plt.plot(x,y, color=colour,linestyle='-',linewidth=1.5)

但這會引發錯誤UnboundLocalError: local variable 'clr' referenced before assignment on line colour = getColour(ci[i], threshold)

我已閱讀其他處理此錯誤的帖子,但我看不出我的問題是什么。

通常,最好是合並您的病情邏輯。 您的clr會具有所有這些價值嗎? 否,因此您可以使用一些elif

def getColour(C, threshold):
    neg = 0 - threshold
    half = threshold/2
    if C <= (neg - half):
        clr = '#2b83ba'
    elif ((neg - half) < C <= neg):
        clr = '#abdda4'
    elif ((threshold + half) > C >= threshold):
        clr = '#fdae61'
    elif (C > (threshold + half)):    
        clr = '#d7191c'
    return clr

那你的病情會成環嗎? 您是否擁有所有案件? 如果是,最好在else拋出錯誤。 如果不是,那意味着您只是忘記了一種情況:

def getColour(C, threshold):
    neg = 0 - threshold
    half = threshold/2
    if C <= (neg - half):
        clr = '#2b83ba'
    elif ((neg - half) < C <= neg):
        clr = '#abdda4'
    elif ((threshold + half) > C >= threshold):
        clr = '#fdae61'
    elif (C > (threshold + half)):    
        clr = '#d7191c'
    else:
        raise ValueError('Value expected for clr')
    return clr

編輯:要回答您的評論,我認為您誤會了我的意思。 在Python中,如果發生意外,最好拋出一個錯誤。 所以:

  • 您提供默認的顏色值,例如“白色”,並且可以正常使用
  • 您返回None,然后其余代碼應在讀取前檢查None值(然后可能會拋出錯誤)
  • 你直接拋出一個錯誤

PEP20:錯誤絕不能默默傳遞。

即使使用“if...else”語句,它也應該失敗。 我留下了許多if s,即使這是不好的做法,只是為了表明這與添加或不添加else無關。 try語句中, except也不會改變它,更不用說finally了。 您需要在任何if塊之前(或在任何try塊或其他級聯代碼塊之前)繪制變量聲明。

方法:

def getColour(C, threshold):
    neg = 0 - threshold
    half = threshold/2
    clr = None
    if C <= (neg - half):
        clr = '#2b83ba'
    if ((neg - half) < C <= neg):
        clr = '#abdda4'
    if ((threshold + half) > C >= threshold):
        clr = '#fdae61'
    if (C > (threshold + half)):    
        clr = '#d7191c'
    return clr

另一種方法是僅在globals() function 返回的字典中返回變量。

def getColour(C, threshold):
    neg = 0 - threshold
    half = threshold/2
    if C <= (neg - half):
        clr = '#2b83ba'
    if ((neg - half) < C <= neg):
        clr = '#abdda4'
    if ((threshold + half) > C >= threshold):
        clr = '#fdae61'
    if (C > (threshold + half)):    
        clr = '#d7191c'
    if 'clr' in `globals()`:
        return clr
    else:
        return None

這不是必需的,因為默認情況下 function 將返回 None :

    else:
        return None

不過,通過對globals()的檢查,它可能會使代碼更具可讀性。

暫無
暫無

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

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