【发布时间】:2013-07-07 06:36:15
【问题描述】:
我试图了解何时以及如何在 Python 中正确使用 super()(2.7.x 或 3.x)
在>>> help(super) 上,解释器告诉我如何称呼它:
class super(object)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound super object; requires issubclass(type2, type)
我知道在 Python3.x 中现在可以在类定义中使用 super(),但我不明白为什么 super(obj) 是不可能的。或类定义中的super(self)。
我知道这一定是有原因的,但我找不到。对我来说,这些行等同于super(obj.__class__, obj) 或super(self.__class__, self),它们可以正常工作吗?
我认为即使在 Python 3.x 中输入 super(obj) 也是一个不错的快捷方式。
【问题讨论】:
-
我本来想说的是,当您在元类上调用
super时会引起歧义,但后来我意识到这种歧义也存在于 2 参数形式中。现在我不确定了。
标签: python inheritance super