Jim
Jim

Reputation: 875

Get class objects from list

If I add a class instance to a multidimensional list:

transList = []

class TransMsg(object):

    def __init__(self):

        self.canID   = ""
        self.msgType = ""
        self.canType = ""
        self.tData0  = ""
        self.tData1  = ""
        self.tData2  = ""
        self.tData3  = ""
        self.tData4  = ""
        self.tData5  = ""
        self.tData6  = ""
        self.tData7  = ""

        self.timer   = 0
        self.DLC     = 0


def addToList():

    global transList

    dictRef = len(transList)
    t = TransMsg()

    t.canID = "FF"
    t.DLC = 8
    t.canType = "s"
    t.msgType = "m"
    t.tData0 = "FF"
    t.tData1 = "FF"
    t.tData2 = "FF"
    t.tData3 = "FF"
    t.tData4 = "FF"
    t.tData5 = "FF"
    t.tData6 = "FF"
    t.tData7 = "FF"
    t.tTimer = "FF"

    transList.append([dictRef,t])

    print transList
    print transList[0][1]


if __name__ == '__main__':

    addToList()

 #output
 # [[0, <__main__.TransMsg object at 0xb75427ac>]]
 # <__main__.TransMsg object at 0xb75427ac>

How would I be able to retrieve the variables stored with in the t instance of TransMsg. For example how would I be able to print the value of tData0 from within the list?

Note: It has to do this through the list variable transList as this is part of a bigger project.

Thanks

Upvotes: 0

Views: 44

Answers (1)

Ionut Hulub
Ionut Hulub

Reputation: 6326

transList[0][1] will give you a reference to the t instance.

transList[0][1].tData0 should work.

Upvotes: 2

Related Questions