Python Method overriding, does signature matter?

后端 未结 6 1100
轮回少年
轮回少年 2020-12-04 15:11

Lets say I have

class Super():
  def method1():
    pass

class Sub(Super):
  def method1(param1, param2, param3):
      stuff

Is this corr

6条回答
  •  一生所求
    2020-12-04 15:44

    In python, all class methods are "virtual" (in terms of C++). So, in the case of your code, if you'd like to call method1() in super class, it has to be:

    class Super():
        def method1(self):
            pass
    
    class Sub(Super):
        def method1(self, param1, param2, param3):
           super(Sub, self).method1() # a proxy object, see http://docs.python.org/library/functions.html#super
           pass
    

    And the method signature does matter. You can't call a method like this:

    sub = Sub()
    sub.method1() 
    

提交回复
热议问题