Inheritance question
Gerard Flanagan
grflanagan at gmail.com
Tue Mar 25 11:24:19 EDT 2008
On Mar 25, 1:34 pm, Tzury Bar Yochay <Afro.Syst... at gmail.com> wrote:
> > Rather than use Foo.bar(), use this syntax to call methods of the
> > super class:
>
> > super(ParentClass, self).method()
>
> Hi Jeff,
> here is the nw version which cause an error
>
> class Foo(object):
> def __init__(self):
> self.id = 1
>
> def getid(self):
> return self.id
>
> class FooSon(Foo):
> def __init__(self):
> Foo.__init__(self)
> self.id = 2
>
> def getid(self):
> a = super(Foo, self).getid()
> b = self.id
> return '%d.%d' % (a,b)
>
> FooSon().getid()
>
> Traceback (most recent call last):
> File "a.py", line 19, in <module>
> FooSon().getid()
> File "a.py", line 14, in getid
> a = super(Foo, self).getid()
> AttributeError: 'super' object has no attribute 'getid'
Use the child class when calling super:
--------------------------------------
class Foo(object):
def __init__(self):
self.id = 1
def getid(self):
return self.id
class FooSon(Foo):
def __init__(self):
Foo.__init__(self)
self.id = 2
def getid(self):
a = super(FooSon, self).getid()
b = self.id
return '%d.%d' % (a,b)
print FooSon().getid()
--------------------------------------
G.
More information about the Python-list
mailing list