Nested Classes and Instances

M

Manuel Graune

Hello,

as an example of what I would like to achieve, think of a street
where each house has a door and a sign with a unique (per house)
number on it. I tried to model this like this:

class House(object):
class Door(object):
def __init__(self,color):
self.color=color
class Sign(object):
def __init__(self,text):
self.text=text
def __init__(self, doorcolor,housenumber):
self.housenumber=housenumber
self.door=House.Door(doorcolor)
self.sign=House.Sign(housenumber)

house1=House("red","1")
house2=House("blue","2")

Well, so far, so good. Now, what I'd like to achive is that the text of
the "sign" changes whenever the variable "housenumber" of the
"parent-instance" changes or that
"house1.sign.text" is a reference/pointer to "house1.housenumber"

Thanks in advance,

Manuel
 
P

Peter Otten

Manuel said:
as an example of what I would like to achieve, think of a street
where each house has a door and a sign with a unique (per house)
number on it. I tried to model this like this:

class House(object):
class Door(object):
def __init__(self,color):
self.color=color
class Sign(object):
def __init__(self,text):
self.text=text
def __init__(self, doorcolor,housenumber):
self.housenumber=housenumber
self.door=House.Door(doorcolor)
self.sign=House.Sign(housenumber)

house1=House("red","1")
house2=House("blue","2")

Well, so far, so good. Now, what I'd like to achive is that the text of
the "sign" changes whenever the variable "housenumber" of the
"parent-instance" changes or that
"house1.sign.text" is a reference/pointer to "house1.housenumber"

Python doesn't support C-style pointers, but you can work around it to some
degree:
.... def __init__(self, housenumber):
.... self.housenumber = housenumber
.... self.sign = Sign(self)
........ def __init__(self, house):
.... self.house = house
.... @property
.... def text(self): return self.house.housenumber
....'42b'

If you are concerned about the tight coupling between House and Sign you can
modify Sign to accept a function that gets the housenumber:
.... def __init__(self, n): self.housenumber = n
........ def __init__(self, gettext):
.... self._gettext = gettext
.... @property
.... def text(self): return self._gettext()
....7

Peter
 
J

J. Cliff Dyer

Hello,

as an example of what I would like to achieve, think of a street
where each house has a door and a sign with a unique (per house)
number on it. I tried to model this like this:

class House(object):
class Door(object):
def __init__(self,color):
self.color=color
class Sign(object):
def __init__(self,text):
self.text=text
def __init__(self, doorcolor,housenumber):
self.housenumber=housenumber
self.door=House.Door(doorcolor)
self.sign=House.Sign(housenumber)

house1=House("red","1")
house2=House("blue","2")

Don't do it like that. Keep your classes independent. Many houses can
have doors, and there's no reason to redefine the concept of Doors for
each one. Same goes for Signs.

class House(object):
def __init__(self, doorcolor, housenumber):
self.door = Door(doorcolor)
self.sign = Sign(housenumber)
self.number = housenumber

class Door(object):
def __init__(self, color):
self.color = color

class Sign(object):
def __init__(self, inscription):
self.inscription = str(housenumber)



(Something like that)

Cheers,
Cliff
 

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,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top