wubbalubba
wubbalubba

Reputation: 764

Weird Behavior in my Python Class

So this is pretty self explanatory.

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

If I run the following statements:

currentTray = Tray()
currentTray.setCycle(250)
currentTray.setCell(300)
print(currentTray)
currentTray.setCell(25)
print(currentTray.cell)

my output would be:

(None, None, False, False)
25

So, I guess I'm trying to figure out why my self.trayData isn't updating according to the values in self.cycle, self.cell etc. What's the problem here?

Upvotes: 1

Views: 239

Answers (3)

Anand S Kumar
Anand S Kumar

Reputation: 90889

When you do the following -

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

self.trayData's first element does not point to self.cycle (atleast not in the way you want it to) , it points to the object self.cycle points to , which is None. So when you set a new value to self.cycle it will not automatically reflect in self.trayData .

The easiest thing to do for your specific case to work is , to set trayData in both setCycle as well as setCell methods.

Example -

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

Or you can define trayData as a property instead of defining it in the __init__() method, Example -

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.

Then you can access trayData using Tray().trayData , using the object of Tray class.

Example/Demo -

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)

Upvotes: 3

SuperBiasedMan
SuperBiasedMan

Reputation: 9969

When you set trayData in __init__ it's not getting updated each time the other functions are called. It would make more sense to make it a method of the class that returns the values:

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

Upvotes: 3

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

Python variables don't update like you're trying to do. You would need to do something like this:

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)

Basically, if you want self.trayData to have the latest data, you need to update it any time you update in the values in it.

Now, if you don't need to ever access self.trayData directly, you could just do:

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

Upvotes: 1

Related Questions