为什么Python允许抽象方法有代码?
Why does Python allow abstract methods to have code?
为什么 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 允许在抽象方法中包含代码?我知道我们可以通过 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 之后添加的任何内容,您不必对此类问题感到疑惑,您可以查看整个讨论。)