Dynamic objects

M

Mark Shewfelt

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
 
T

Tim

Mark said:
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
I don't see how your examples could work, helps if you post the actual code.
Try these classes, I think they accomplish what your trying to do.

class Building:
def __init__(self, data = ''):
self.data = data
self.equipment = {}
def AddEquipment(self, name, data ):
self.equipment[ name ] = Equipment( data )

class Equipment:
def __init__(self, data = ''):
self.data = data
self.components = {}
def AddComponent(self, name, data ):
self.components[ name ] = Component( data )

class Component:
def __init__(self, data):
self.data = data
 
M

Mark Shewfelt

Thanks a lot Tim!

My __init__ functions didn't set the dictionaries like you did below
(e.g. self.equipment = {} ).

Newbie mistake - won't make that one again.

Thanks again,

Mark

Mark said:
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
I don't see how your examples could work, helps if you post the actual code.
Try these classes, I think they accomplish what your trying to do.

class Building:
def __init__(self, data = ''):
self.data = data
self.equipment = {}
def AddEquipment(self, name, data ):
self.equipment[ name ] = Equipment( data )

class Equipment:
def __init__(self, data = ''):
self.data = data
self.components = {}
def AddComponent(self, name, data ):
self.components[ name ] = Component( data )

class Component:
def __init__(self, data):
self.data = data
 
L

Larry Bates

Mark said:
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
With the way you defined Building multiple buildings would share
equipment dictionary as it is defined as a class variable and you
want an instance variable (I'm pretty sure). You probably wanted
(not tested):

class Building:
def __init__(self):
self.equipment = {}

def AddEquipment(name, data):
equipment[name]=Equipment(data)

same for Equipment class and Component class.

class Equipment:
def __init__(self):
self.components={}

def AddComponent(name, data):
components[name]=Component(data)


class Component:
def __init__(self, data)
self.data=data


-Larry Bates
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top