Calling a base class's classmethod in Python

后端 未结 4 1872
独厮守ぢ
独厮守ぢ 2020-12-12 23:20

Consider the following code:

class Base(object):

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

class Derived(Base):

    @classmethod
    def d         


        
4条回答
  •  情歌与酒
    2020-12-12 23:43

    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.

提交回复
热议问题