Dictionary of classes

Robert Sherwood foofboy at speakeasy.net
Sat Dec 1 11:45:29 EST 2001


Greetings, all.

I would like to construct and use a dictionary containing object
references. I'm translating a binary file stream to an object
structure, and I'd like to use the parts of the binary stream that
identify the data as keys in a dictionary which will return an object
reference which the main program can then instantiate and populate from
the data stream. I know that it's possible to store objects as the value
in a dictionary. Has anyone seen this done in the wild. Best of all 
would be a pointer to some code implementing such a feature.

Any help would be appreciated. An example of what I'm trying is attached
below.

Thanks, 

Robert Sherwood

    frameID = self.headerArray[:4].toString()
    try:
        frame = objectDict[frameID]()
        frame.decode(self.popFrame())
        self.frames[frame.getFrameID()] = frame
    except KeyError:
        frame = unknownFrame()
        frame.decode(self.popFrame())
        self.frames[frame.getFrameID()] = frame

where the objectDict looks like this.

objectDict = {
    '0001':object1,
    '0002':object2,
    '0003':object3,
}



More information about the Python-list mailing list