Dynamic objects
Mark Shewfelt
mshewfelt at gmail.com
Thu Aug 17 15:04:13 EDT 2006
Hello,
I have implemented a series of classes representing a Building, its
respective Equipment, and then various Components of that equipment
like so (as you'll be able to tell, I'm a newbie):
class Building:
equipment = {}
def AddEquipment( name, data ):
equipment[ name ] = Equipment( data )
class Equipment:
components = {}
def AddComponent( name, data ):
components[ name ] = Component( data )
class Component:
data = ""
These classes are used like so:
test = Building()
test.AddEquipment( "equipment 1", data )
test.AddEquipment( "equipment 2", data )
test.equipment["equipment 1"].AddComponent( "component 1", data )
test.equipment["equipment 1"].AddComponent( "component 2", data )
test.equipment["equipment 2"].AddComponent( "component 3", data )
But it appears as though the instance of "equipment 1" has ALL of the
components in its components dictionary. I was hoping that the
test.equipment["equipment 1"].components dictionary would only have
those components that were assigned to "equipment 1".
I have implemented __init__ functions for all of the classes, but all
they do is initialize some data that I haven't shown here.
I think I'm trying to use a C++ way of doing this (without the new
operator) so if anyone would be so kind as to help with the Python way
of doing this sort of thing I will be eternally grateful.
Cheers,
Mark Shewfelt
More information about the Python-list
mailing list