I am creating a package with a lot of nested classes. Imagine 2 classes: House and Roof. The House can instantiate a Roof, as well as store it, change its properties, and call its methods. But how about the reverse problem? Can the Roof object find out if it was instantiated by a House and discover anything about that parent object? My hypothetical parent House object is <parent>
in this pseudo-code:
class House(object):
def __init__(self, style:str):
self.style = style
self.roof = Roof()
class Roof(object):
def __init__(self):
self.type = None
if <parent>:
if <parent>.style = 'Rambler':
self.type = 'Open Gable'
elif <parent>.style = 'Bungalow':
self.type = 'Hip'
h = House('Bungalow')
This is just a wild guess at how this might work, but I'm testing to see if a parent class exists, and then want to access its properties. Is this possible?
I know I can pass one parameter (the House's style) to the Roof's __init__
, but the real problem I'm trying to solve involves a LOT more properties, which is what I want to avoid.
I have seen packages that solve this by having the Roof class store a property __house
, which I presume is to solve this problem. I assume the House passes self
to the Roof constructor, but that seems like more coding and I also wonder if it duplicates the objects stored by the program.
thanks!