Python方法覆盖,签名是否重要? [英] Python Method overriding, does signature matter?

查看:104
本文介绍了Python方法覆盖,签名是否重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有

class Super():
  def method1():
    pass

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

这是正确的吗?对method1的调用总是会转到子类吗?我的计划是让2个子类重写方法1,使用不同的参数

Is this correct? Will calls to method1 always go to the sub class? My plan is to have 2 sub classes each override method1 with different params

推荐答案

Python将允许这样做,但是如果 method1()旨在从外部代码执行,然后你可能想重新考虑这个,因为它违反了 LSP 等并不总能正常工作。

Python will allow this, but if method1() is intended to be executed from external code then you may want to reconsider this, as it violates LSP and so won't always work properly.

这篇关于Python方法覆盖,签名是否重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆