Simon Hibbs <[EMAIL PROTECTED]> writes: > Orriginaly I thought I'd need to have a hull object which contains > component objects, but the component objects need access to members > of the hull object (e.g. the hull size) so that looks messy to > implement.
Was it as messy as this:: class ShipHull(object): def __init__(self, size): self.components = dict() self.size = size class ShipComponent(object): def __init__(self, name, hull): self.name = name self.hull = hull > I have defined a base class Component with a class member variable > 'hull_size' so that all components can see the hull size. It seems to me the hull is an attribute of the component, and the size is an attribute of the hull. Why would the hull size be a *class* attribute? > I've then got two child classes called Fixed_Component and Percent > _Component that implement their mass, mass_percent and cost > properties appropriately for their type. class FixedShipComponent(ShipComponent): def _get_mass(self): return calculation_foo(self.hull.size) mass = property(_get_mass) def _get_cost(self): return calculation_bar(self.hull.size) cost = property(_get_cost) class PercentShipComponent(ShipComponent): def _get_mass(self): return calculation_spam(self.hull.size) mass = property(_get_mass) def _get_cost(self): return calculation_eggs(self.hull.size) cost = property(_get_cost) > I've also defined a Hull class which also inherits from Component > and provides methods for access to the hull_size class variable. I don't see why, if a ShipComponent needs to refer to its hull as something special, that a ShipHull would subclass ShipComponent. > Is there a way to cleanly implement a parent-child relationship > between objects that gives child objects limited access to members > of the parent? Sure; have the child instance grow an attribute referencing the parent, preferably by passing it to the initialisation function (__init__) of the child. -- \ Rommel: “Don't move, or I'll turn the key on this can of Spam!” | `\ —The Goon Show, _Rommel's Treasure_ | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list