Python super() 参数 : why not super(obj)?

标签 python inheritance super

我试图了解何时以及如何在 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) 也是一个不错的快捷方式。

最佳答案

只有Python 2才需要二元形式。原因是self.__class__总是指继承树中的“叶子”类——也就是最具体的类对象——但是当你调用 super 时你需要告诉它当前正在调用哪个实现,这样它就可以调用继承树中的下一个实现。

假设你有:

class A(object):
   def foo(self):
      pass

class B(A):
   def foo(self):
      super(self.__class__, self).foo()

class C(B):
   def foo(self):
      super(self.__class__, self).foo()

c = C()

请注意,c.__class__ 始终是 C。现在想想如果你调用 c.foo() 会发生什么。

当你在 C 的方法中调用 super(self.__class__, self) 时,会像调用 super(C, self),意思是“调用C 继承的此方法的版本”。这将调用 B.foo,这很好。但是当你从B调用super(self.__class__, self)时,还是和调用super(C, self)一样,因为是同一个self,所以 self.__class__ 仍然是 C。结果是 B 中的调用会再次调用 B.foo 并发生无限递归。

当然,您真正想要的是能够调用 super(classThatDefinedTheImplementationThatIsCurrentlyExecuting, self),这实际上是 Python 3 super() 所做的。

在 Python 3 中,您只需执行 super().foo() 即可,它会做正确的事。我不清楚您对 super(self) 作为快捷方式的意思。在 Python 2 中,由于我上面描述的原因,它不起作用。在 Python 3 中,这将是一个“longcut”,因为您可以只使用普通的 super()

super(type)super(type1, type2) 在 Python 3 中可能偶尔仍需要使用,但对于不寻常的情况,这些总是更深奥的用法。

关于Python super() 参数 : why not super(obj)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17509846/

相关文章:

javascript - 访问由 Object.create 创建的对象中的父( super )方法

objective-c - 编译器在找到 [super msg] 时会做什么;

C++ 在作为函数传递后从父级转换为子级?

python - 保存动画时未清除前一帧

python - 在任意导入函数内调用 globals() 函数?

python - 我无法理解奇怪的行为

arrays - 协议(protocol)扩展错误数组

Python 可继承函数

Javascript 继承调用父级的非默认构造函数

python - numpy中二维的跨步卷积