Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52847
| References | <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> <5B80DD153D7D744689F57F4FB69AF47418683AD8@SCACMX008.exchad.jpmchase.net> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2013-08-22 15:59 -0600 |
| Subject | Re: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.143.1377208801.19984.python-list@python.org> (permalink) |
On Thu, Aug 22, 2013 at 3:26 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com.dmarc.invalid> 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)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Bitswapper <bithead0101@gmail.com> - 2013-08-22 14:03 -0700
RE: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-08-22 21:26 +0000
Re: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Bitswapper <bithead0101@gmail.com> - 2013-08-22 15:00 -0700
Re: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Bitswapper <bithead0101@gmail.com> - 2013-08-23 07:11 -0700
Re: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-22 15:59 -0600
Re: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? Bitswapper <bithead0101@gmail.com> - 2013-08-23 12:39 -0700
RE: Can a child access parent attributes if that child added post-hoc as an attribute to the parent? "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> - 2013-08-27 18:38 +0000
csiph-web