Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52844 > unrolled thread
| Started by | Bitswapper <bithead0101@gmail.com> |
|---|---|
| First post | 2013-08-22 14:03 -0700 |
| Last post | 2013-08-27 18:38 +0000 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Bitswapper <bithead0101@gmail.com> |
|---|---|
| Date | 2013-08-22 14:03 -0700 |
| Subject | Can a child access parent attributes if that child added post-hoc as an attribute to the parent? |
| Message-ID | <14d50efb-e88b-4259-960a-97ef58e345d8@googlegroups.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
[toc] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> |
|---|---|
| Date | 2013-08-22 21:26 +0000 |
| Message-ID | <mailman.142.1377207491.19984.python-list@python.org> |
| In reply to | #52844 |
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
>
> 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
I not sure what you mean by the above. Can you provide an example of what you want
to occur and the output for it?
~Ramit
This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [next] | [standalone]
| From | Bitswapper <bithead0101@gmail.com> |
|---|---|
| Date | 2013-08-22 15:00 -0700 |
| Message-ID | <f8eda855-63c0-4a3f-bae1-fc83eb8d01b7@googlegroups.com> |
| In reply to | #52846 |
On Thursday, August 22, 2013 4:26:24 PM UTC-5, 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
>
> >
>
> > 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
>
>
>
> I not sure what you mean by the above. Can you provide an example of what you want to occur and the output for it?
>
I was thinking of:
map = Map('myMap')
map.rules[1] = Rule[1]
map.rules[2] = Rule[2]
>>> print map.rules[1]
>>> Map myMap rule number 1
>>> print map.rules[2]
>>> Map myMap rule number 2
>>>
>>> map.mapName = "newname"
>>> print map.rules[1]
>>> Map newname rule number 1
>>> print map.rules[2]
>>> Map newname rule number 2
[toc] | [prev] | [next] | [standalone]
| From | Bitswapper <bithead0101@gmail.com> |
|---|---|
| Date | 2013-08-23 07:11 -0700 |
| Message-ID | <39447032-7220-4350-b24a-62f2810c230e@googlegroups.com> |
| In reply to | #52848 |
On Thursday, August 22, 2013 5:00:38 PM UTC-5, Bitswapper wrote:
> On Thursday, August 22, 2013 4:26:24 PM UTC-5, 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
>
> >
>
> > >
>
> >
>
> > > 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
>
> >
>
> >
>
> >
>
> > I not sure what you mean by the above. Can you provide an example of what you want to occur and the output for it?
>
> >
>
>
>
> I was thinking of:
>
>
>
> map = Map('myMap')
>
> map.rules[1] = Rule(1)
>
> map.rules[2] = Rule(2)
>
>
>
> >>> print map.rules[1]
>
> >>> Map myMap rule number 1
>
> >>> print map.rules[2]
>
> >>> Map myMap rule number 2
>
> >>>
>
> >>> map.mapName = "newname"
>
> >>> print map.rules[1]
>
> >>> Map newname rule number 1
>
> >>> print map.rules[2]
>
> >>> Map newname rule number 2
Or rather:
map = Map('myMap')
map.rules[1] = Rule(1)
map.rules[2] = Rule(2)
>>> print map.rules[1]
>>> Map myMap rule number 1
>>> print map.rules[2]
>>> Map myMap rule number 2
>>>
>>> map.mapName = "newname"
>>> print map.rules[1]
>>> Map newname rule number 1
>>> print map.rules[2]
>>> Map newname rule number 2
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-08-22 15:59 -0600 |
| Message-ID | <mailman.143.1377208801.19984.python-list@python.org> |
| In reply to | #52844 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Bitswapper <bithead0101@gmail.com> |
|---|---|
| Date | 2013-08-23 12:39 -0700 |
| Message-ID | <94549b6a-908b-4d7b-bc2c-23ef22532ba8@googlegroups.com> |
| In reply to | #52847 |
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!
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com.dmarc.invalid> |
|---|---|
| Date | 2013-08-27 18:38 +0000 |
| Message-ID | <mailman.277.1377628719.19984.python-list@python.org> |
| In reply to | #52844 |
Ian Kelly wrote:
> 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.
>
Good point. Composition definitely makes more sense as I was
confused by how the inheritance was supposed to work anyway. :)
~Ramit
This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web