2013-11-26 35 views
0

因此,我認爲我對OOP的東西持保留態度,但是當我認爲我理解它時,會發生意想不到的事情。我將一個已經在一個函數中創建的變量分配給另一個函數,然後改變它(注意,這是一個全局的而不是,我已經吸取了我的教訓)。但我仍然遇到一個未綁定的本地錯誤,這讓我感到困惑。我已閱讀了這裏和其他地方關於此錯誤的大部分文檔,並且我沒有找到對此特定問題的答案。「無約束本地錯誤」,訪問另一個函數中的變量

這裏的錯誤消息:

Traceback (most recent call last) 
File "C:\Users\Tony DiBiase\AppData\Local\ESRI\Desktop10.2\AssemblyCache\{2801A62B-53E6-17B4-D465-BB6072FDEEF9}\parameterizer_final_addin.py", line 115, in onClick 
num = domainNumber.text 
UnboundLocalError: local variable 'domainNumber' referenced before assignment 

這裏就是我試圖做(編輯了爲簡潔的外源性部分):

class domainNumber(object): 
"""Implementation for parameterizer_final_addin.domainNumber (ComboBox)""" 
    def __init__(self): 
     self.items = ["3000", "1000", "200", "100"] 
     self.editable = True 
     self.enabled = True 
     self.dropdownWidth = 'WWWWWW' 
     self.width = 'WWWWWW' 
     self.hinttext = "Cell resolution (m^2)" 
    def onEditChange(self, text): 
     #so pass the text changed to a domainNumber.text, which can be passed to other functions 
     self.text = text 

class printFinal(): 
    """Implementation for parameterizer_final_addin.printFinal""" 
    def __init__(self): 
     self.enabled = True 
     self.checked = False 
    def onClick(self): 
     #iterate through domains, using number set in domainNumber 
     counter = 0 
     num = domainNumber.text #So calling the variable from the other function 
     num = num-1 
     #run the business logic for the esri pythonaddin 
     while counter < num: 
      ~do stuff 
      num+=1 #to iterate through and finish the loop 

那麼什麼是很明顯的是,錯誤當我嘗試在printFinal類中嘗試alterdomainNumber.text變量時發生。但這就是讓我困惑的原因,因爲printFinal類不是試圖改變全局變量,而當我將domainNumber.text放入printFinal類中時,它應該是printFinal命名空間中的本地(並因此可編輯)?我的意思是,我特別將domainNumber.text存入一個局部變量,然後改變而不是試圖改變domainNumber.text本身。

,對嗎?我從我的代碼中的其他函數訪問變量,並且不會遇到這個問題,這只是因爲我在這裏遇到問題時將其減1。我怎樣才能做到這一點,而不僅僅是使全局變量(我試圖避免)?

+0

的錯誤是不存在的。你在哪裏使用'domainNumber'? – aIKid

+0

抱歉,意外地複製了錯誤的類(它做了類似的事情),在這個例子中,setRes和domainNumber完全一樣,我將改變這個問題來反映 – user22861

回答

0

domainNumber是
text是domainNumber實例上的一個屬性。

爲了得到它的工作,你應該創建一個類型domainNumber的對象:

dn = domainNumber() 
dn.onEditChange("123") 

...

num = dn.text 
相關問題