在我的Python课上有奇怪的行为

2024-04-26 08:08:45 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这是不言自明的。你知道吗

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

所以,我想我在想为什么水螅没有根据中的值进行更新自循环, 自身细胞等等,这里有什么问题?你知道吗


Tags: selfnonefalsedatadefnotcellclass
3条回答

当您执行以下操作时-

self.trayData = (self.cycle,
                 self.cell,
                 self.bIsTilted,
                 self.bSendNoData)

self.trayData的第一个元素没有指向self.cycle(至少不是您希望的方式),它指向self.cycle指向的对象,即None。因此,当您将新值设置为self.cycle时,它不会自动反映在self.trayData。你知道吗

要使您的特定案例正常工作,最简单的方法是在setCyclesetCell方法中设置trayData。你知道吗

示例-

def setCycle(self, cycle):
    self.cycle = cycle
    self.trayData = (self.cycle,
                 self.cell,
                 self.bIsTilted,
                 self.bSendNoData)

或者可以将trayData定义为属性,而不是在__init__()方法中定义它,例如-

class Tray(object):
    '''
    A class used to collect information about each unique tray
    '''
    @property
    def trayData(self):
        return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData)
    .
    .
    . #rest of the class.

然后可以使用Tray().trayData,使用Tray类的对象来访问trayData。你知道吗

示例/演示-

In [94]: class Tray(object):
   ....:     @property
   ....:     def trayData(self):
   ....:         return (1,2,3,4)
   ....:

In [95]: t=Tray()

In [96]: t.trayData
Out[96]: (1, 2, 3, 4)

当您在__init__中设置trayData时,它不会在每次调用其他函数时都得到更新。使其成为返回值的类的方法更有意义:

def trayData():
    return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData)

Python变量不会像您尝试的那样更新。你需要这样做:

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
    self.trayData = (self.cycle,
                     self.cell,
                     self.bIsTilted,
                     self.bSendNoData)

def setCell(self, cell):
    self.cell = cell
    self.trayData = (self.cycle,
                     self.cell,
                     self.bIsTilted,
                     self.bSendNoData)

基本上,如果您希望self.trayData拥有最新的数据,那么您需要在更新其中的值时随时更新它。你知道吗

现在,如果您不需要直接访问self.trayData,您可以:

def __repr__(self):
    return (self.cycle, self.cell, self.bIsTilted, self.bSendNoData)

相关问题 更多 >