Python Method overriding, does signature matter?

后端 未结 6 1150
轮回少年
轮回少年 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:32

    It will work:

    >>> class Foo(object):
    ...   def Bar(self):
    ...     print 'Foo'
    ...   def Baz(self):
    ...     self.Bar()
    ... 
    >>> class Foo2(Foo):
    ...   def Bar(self):
    ...     print 'Foo2'
    ... 
    >>> foo = Foo()
    >>> foo.Baz()
    Foo
    >>> 
    >>> foo2 = Foo2()
    >>> foo2.Baz()
    Foo2
    

    However, this isn't generally recommended. Take a look at S.Lott's answer: Methods with the same name and different arguments are a code smell.

提交回复
热议问题