python - 为什么Python允许抽象方法有代码?

标签 python abstract

为什么 python 允许在抽象方法中包含代码?我知道我们可以通过 super 调用该代码,但我想不出为什么我希望我的抽象方法中包含代码。

最佳答案

这在 abc module docs 中有解释:

Note: Unlike Java abstract methods, these abstract methods may have an implementation. This implementation can be called via the super() mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance.

(强调已添加。)

在引入 ABC 的 PEP 中也对此进行了解释,您可以从 PEP 到 python-dev 和 -ideas 线程的链接,这些线程更详细地讨论了该决定。 (这是开放式开发语言的优势之一;对于大约 2.2 之后添加的任何内容,您不必对此类问题感到疑惑,您可以查看整个讨论。)

关于python - 为什么Python允许抽象方法有代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29917870/

相关文章:

python - 新行字符在 Pygame 窗口的格式化输出中不起作用

c# - 如何强制继承类在 C# 中实现静态方法?

c++ - 私有(private)纯虚函数的目的?

Java:OOP 建议和改进小型应用程序的建议

Angular4 组件继承与抽象类

python - Django 导入导出字段

python - 在我的本地盒子上运行我的 django webapp 时,为什么我使用 firefox 7.0.1 时出现黑屏

python - 尝试将 3 个列表中的元素组合(连接)为一个新列表

python - 将多个数据框导出为 html

c# - 抽象泛型隐式转换为派生类型,错误?