繁体   English   中英

我的Python类中的怪异行为

[英]Weird Behavior in my Python Class

因此,这很容易解释。

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.cell等中的值进行更新。这是什么问题?

当您执行以下操作时-

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类的对象使用Tray().trayData访问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更新其中的值时都需要对其进行更新。

现在,如果您不需要直接访问self.trayData ,则可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM