> Dear sirs,
> I am only begining Python and this problem has confused me for long.
> question: How do I upcall parents methods? I mean,
>
> class p:
> def m(self):
> print "This is parent method"
> class c(p):
> def m(self):
> # I want to override m by first calling p.m and print something else
>
> how do I write c.m()??
> thanks
>
class p:
def m(self):
print "This is parent method"
class c(p):
def m(self):
p.m(self)
print "... and this is subclass method"
>>> x = c()
>>> x
<c instance at 20068078>
>>> x.m
<method c.m of c instance at 20068078>
>>> x.m()
This is parent method
... and this is subclass method
>>>
You can also go uplevel dynamically -
instance.__class__ is the class
instance.__class__.__bases__ is the tuple of that classes super-classes.
>>> x.__class__
<class c at 200680e8>
>>> x.__class__.__bases__
(<class p at 20068128>,)
>>> x.__class__.__bases__[0].m
<unbound method p.m>
>>>
But this can be tricky, Guido doesn't recommend it, and there's
probably very few cases where you need to do it ( i.e. not explicitly
name the parent/super class. )
- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics