Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52844
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2013-08-22 14:03 -0700 |
| Message-ID | <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.com> (permalink) |
| Subject | Can a child access parent attributes if that child added post-hoc as an attribute to the parent? |
| From | Bitswapper <bithead0101@gmail.com> |
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
def __repr__(self):
return "Map " + self.mapName + " rule number " + str(self.number)
if __name__ == "__main__":
map = Map("thismap")
rule = Rule(1)
map.rules[rule.number] = rule
with the above:
$ python -i inherit.py
>>> map
<__main__.Map object at 0xb7e889ec>
>>> map.rules
{1: Map rule number 1}
>>> map.rules[1]
Map rule number 1
>>>
I have tried adding:
map.rules[2] = Rule(2)
but that still gets:
$ python -i inherit.py
>>> map.rules
{1: Map rule number 1, 2: Map rule number 2}
>>>
and:
map.rule = Rule(3)
which also doesn't really get me what I'm looking for:
>>> map.rules
{1: Map rule number 1, 2: Map rule number 2}
>>> map.rule
Map rule number 3
>>>
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.
Any thoughts are welcome, and thanks in advance
Back to comp.lang.python | Previous | Next — 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