【发布时间】:2013-11-26 03:47:34
【问题描述】:
所以我认为我掌握了 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
所以非常明显的是,当我尝试更改 domainNumber.text 类中的domainNumber.text 变量时会发生错误。但这让我感到困惑,因为 printFinal 类并没有尝试更改全局变量,当我调用 domainNumber.text inside printFinal 类时,它应该是本地的(并且因此可编辑)在printFinal 命名空间中?我的意思是,我专门将 domainNumber.text 保存到一个局部变量,然后改变 那个 而不是试图改变 domainNumber.text 本身。
对吗?我从代码中其他地方的其他函数访问变量,并没有遇到这个问题,只是因为我在这里将它减去 1 才遇到问题。我怎样才能做到这一点,而不仅仅是使变量成为全局变量(我试图避免)?
【问题讨论】:
-
不存在错误。你在哪里使用
domainNumber? -
抱歉,不小心复制了错误的类(执行类似操作。在这种情况下,setRes 和 domainNumber 完全相同。我将更改问题以反映这一点
标签: python oop python-2.7 gis arcgis