Sridhar Ratnakumar Sridhar Ratnakumar - 10 months ago 41
Python Question

Calling a base class's classmethod in Python

Consider the following code:

class Base(object):

def do(cls, a):
print cls, a

class Derived(Base):

def do(cls, a):
print 'In derived!'
#, a) -- can't pass `cls`

if __name__ == '__main__':
d = Derived()'hello')

> $ python
> In derived!
> <class '__main__.Base'> msg

, how do I call

I would normally use
or even the base class name directly if this is a normal object method, but apparently I can't find a way to call the classmethod in the base class.

In the above example,
class instead of

Answer Source
super(Derived, cls).do(a)

EDIT: Oh, wait a minute... it's not clear exactly what you're asking. This is how you would invoke the code in the base class's version of the method, from the derived class.