Can a child access parent attributes if that child added post-hoc asan attribute to the parent?

B

Bitswapper

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.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
and:
map.rule = Rule(3)

which also doesn't really get me what I'm looking for:


It seems to me what I'm trying to do is link an arbitrary child instance toan arbitrary instance of a parent class, which in this case would be handyBecause 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
 
P

Prasad, Ramit

Bitswapper said:
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.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
and:
map.rule = Rule(3)

which also doesn't really get me what I'm looking for:


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 notsure 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 andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 
B

Bitswapper

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):

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
<__main__.Map object at 0xb7e889ec>
{1: Map rule number 1}

Map rule number 1
I have tried adding:
map.rules[2] = Rule(2)
but that still gets:
$ python -i inherit.py
{1: Map rule number 1, 2: Map rule number 2}

map.rule = Rule(3)
which also doesn't really get me what I'm looking for:
{1: Map rule number 1, 2: Map rule number 2}
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
 
B

Bitswapper

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):
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
<__main__.Map object at 0xb7e889ec>
{1: Map rule number 1}
Map rule number 1
I have tried adding:
map.rules[2] = Rule(2)
but that still gets:
$ python -i inherit.py
{1: Map rule number 1, 2: Map rule number 2}
map.rule = Rule(3)
which also doesn't really get me what I'm looking for:
{1: Map rule number 1, 2: Map rule number 2}
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
 
B

Bitswapper

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.






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!
 
P

Prasad, Ramit

Ian said:
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 andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top