On Thursday, August 22, 2013 4:59:17 PM UTC-5, Ian wrote: > On Thu, Aug 22, 2013 at 3:26 PM, Prasad, Ramit > > <> wrote: > > > Bitswapper wrote: > > >> > > >> So I have a parent and child class: > > >> > > >> > > >> class Map(object): > > >> def __init__(self, name=''): > > >> self.mapName = name > > >> self.rules = {} > > >> > > >> class Rule(Map): > > >> def __init__(self, number): > > >> Map.__init__(self) > > >> self.number = number > > > > > > This means that rules will never have a name. I think you need > > > def __init__(self, name='', number=None): > > > Map.__init__(self, name) > > > self.number = number > > > > No, that's still wrong. The OP talks abut maps having names, not > > rules having names. Unless a Rule is-a Map, which sounds unlikely, > > Rule should not be inheriting from Map in the first place. > > > > >> It seems to me what I'm trying to do is link an arbitrary child instance > >> to an arbitrary instance of a > > >> parent class, which in this case would be handy Because I'd like to > >> populate a map with rules and > > >> print the rules including the parent map name for each rule. I'm just not > >> sure how I would go about > > >> doing this in python. > > > > You'll need to keep a reference to the Map on each Rule instance. So > > instead of self.mapName you'll have self.map.mapName. Your Rule class > > should probably look something like this: > > > > class Rule(object): > > def __init__(self, map, number): > > self.map = map > > self.number = number > > > > And then when you construct it you'll need to tell it what map it belongs to: > > > > rule = Rule(map, 1)
Actually yea, that makes sense. I was looking for a way for a child to 'automagically' inherit parent instance-specific data via inheritance only by virtue of being a child of a parent instance. What you're suggesting makes more sense. Thanks! -- http://mail.python.org/mailman/listinfo/python-list