131

Consider the following code:

class Base(object):

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

class Derived(Base):

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

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

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

From Derived.do, how do I call Base.do?

I would normally use super 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, Base.do(a) prints Base class instead of Derived class.

1

4 Answers 4

146

If you're using a new-style class (i.e. derives from object in Python 2, or always in Python 3), you can do it with super() like this:

super(Derived, cls).do(a)

This is how you would invoke the code in the base class's version of the method (i.e. print cls, a), from the derived class, with cls being set to the derived class.

5
  • 8
    uh uh .. how come it never occured to me that I can use super on classmethods too. Commented Aug 12, 2009 at 23:11
  • this only works (due to a limitation imposed by super) if the base derives from object, right? what do you do if that's not the case? Commented Jun 11, 2014 at 19:06
  • Yeah, this only works for new-style classes, which derive from object. (at least in Python 2, but in Py3 I think all classes are new-style, IIRC) Otherwise you have to do Base.do(self, ...), I think, thereby hard-coding the name of the superclass.
    – David Z
    Commented Jun 11, 2014 at 19:29
  • Inside Derived.do(), isn't cls the same as Derived?
    – Ray
    Commented Oct 16, 2017 at 17:01
  • @Ray If it is actually an instance of Derived but not of a subclass, then yes.
    – David Z
    Commented Oct 16, 2017 at 17:02
17

this has been a while, but I think I may have found an answer. When you decorate a method to become a classmethod the original unbound method is stored in a property named 'im_func':

class Base(object):
    @classmethod
    def do(cls, a):
        print cls, a

class Derived(Base):

    @classmethod
    def do(cls, a):
        print 'In derived!'
        # Base.do(cls, a) -- can't pass `cls`
        Base.do.im_func(cls, a)

if __name__ == '__main__':
    d = Derived()
    d.do('hello')
2
  • 2
    Note: This approach works for old style classes where super() doesn't work
    – Alex Q
    Commented Jun 2, 2011 at 20:36
  • 3
    Also available as __func__ in python 2.7 and 3
    – dtheodor
    Commented Apr 29, 2014 at 20:09
3

Building on the answer from @David Z using:

super(Derived, cls).do(a)

Which can be further simplified to:

super(cls, cls).do(a)

I often use classmethods to provide alternative ways to construct my objects. In the example below I use the super functions as above for the class method load that alters the way that the objects are created:

class Base():
    
    def __init__(self,a):
        self.a = a
    
    @classmethod
    def load(cls,a):
        return cls(a=a)
    
class SubBase(Base): 

    @classmethod
    def load(cls,b):
        a = b-1
        return super(cls,cls).load(a=a)
    
base = Base.load(a=1)
print(base)
print(base.a)

sub = SubBase.load(b=3)
print(sub)
print(sub.a)

Output:

<__main__.Base object at 0x128E48B0>
1
<__main__.SubBase object at 0x128E4710>
2
3
  • 1
    Replacing super(Derived, cls).do(a) with super(cls, cls).do(a) is not a good way. It will not work (it actually will cause an exception) when you have another class SubSubBase(SubBase) without load() method defined and you will try to use it. Commented Feb 1, 2021 at 9:33
  • @MartinGrůber: so what is the correct way in Python3? Simply super().load(...) will work correctly for classmethods?
    – MestreLion
    Commented Oct 7, 2021 at 12:06
  • @MestreLion Not sure what is the correct way but I would stay with super(Derived, cls).do(a) which works AFAIK. Commented Oct 8, 2021 at 13:07
-4

This works for me:

Base.do('hi')
2
  • 9
    The cls argument will then be bound to Base instead of Derived Commented Aug 12, 2009 at 23:14
  • what works for me is this - which looks (a lot) like Ned's answer: where self derives from QGraphicsView which has paintEvent(QPaintEvent) def paintEvent (self, qpntEvent): print dir(self) QGraphicsView.paintEvent(self, qpntEvent)
    – user192127
    Commented Mar 31, 2012 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.