所以这是不言自明的。你知道吗
class Tray(object):
'''
A class used to collect information about each unique tray
'''
def __init__(self):
self.cycle = None
self.cell = None
self.bIsTilted = False # Assume trays are not tilted
self.bSendNoData = False # Assume data is sent, modified later if data not sent
self.trayData = (self.cycle,
self.cell,
self.bIsTilted,
self.bSendNoData)
def __repr__(self):
return str(self.trayData)
def setCycle(self, cycle):
self.cycle = cycle
def setCell(self, cell):
self.cell = cell
如果我运行以下语句:
currentTray = Tray()
currentTray.setCycle(250)
currentTray.setCell(300)
print(currentTray)
currentTray.setCell(25)
print(currentTray.cell)
我的输出是:
(None, None, False, False)
25
所以,我想我在想为什么水螅没有根据中的值进行更新自循环, 自身细胞等等,这里有什么问题?你知道吗
当您执行以下操作时-
self.trayData
的第一个元素没有指向self.cycle
(至少不是您希望的方式),它指向self.cycle
指向的对象,即None
。因此,当您将新值设置为self.cycle
时,它不会自动反映在self.trayData
。你知道吗要使您的特定案例正常工作,最简单的方法是在
setCycle
和setCell
方法中设置trayData
。你知道吗示例-
或者可以将
trayData
定义为属性,而不是在__init__()
方法中定义它,例如-然后可以使用
Tray().trayData
,使用Tray
类的对象来访问trayData。你知道吗示例/演示-
当您在
__init__
中设置trayData
时,它不会在每次调用其他函数时都得到更新。使其成为返回值的类的方法更有意义:Python变量不会像您尝试的那样更新。你需要这样做:
基本上,如果您希望
self.trayData
拥有最新的数据,那么您需要在更新其中的值时随时更新它。你知道吗现在,如果您不需要直接访问
self.trayData
,您可以:相关问题 更多 >
编程相关推荐