Marcus Goldfish said unto the world upon 28/06/2005 00:58:
Hi,
The following example doesn't work as I would like-- the child
instance doesn't expose the attribute set in the parent. Can someone
point out what I am missing?
Thanks,
Marcus
class Parent(object):
def __init__(self, name=I am a parent):
self.name = name
class Child(Parent):
def __init__(self, number):
super(Parent, self).__init__(I am a child)
self.number = number
# I would like it to produce the following:
c = Child(23)
c.number
23
c.name
I am a child
# but I 'AttributeError: 'Child' object has no attribute 'name''
Hi Marcus,
Try it this way:
class Parent(object):
def __init__(self, name=I am a parent):
self.name = name
class Child(Parent):
def __init__(self, number):
# Note change here in super()
super(Child, self).__init__(Finally, I'm a child!)
self.number = number
c = Child(42)
c.number
42
c.name
Finally, I'm a child!
Take a look at these classes, and perhaps they will help clear up any
residual puzzlement.
class A(object):
def do_it(self):
print From A
class B(A):
def do_it(self):
print From B
class C(B):
def do_it(self):
# look for do_it in B's superclass (i.e. A)
super(B, self).do_it()
# look for do_it in C's superclass (i.e. B)
super(C, self).do_it()
c = C()
c.do_it()
From A
From B
HTH,
Brian vdB
___
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor