使用Python的super()
做方法链时,必须显式指定自己的类,例如:
class MyDecorator(Decorator):
def decorate(self):
super(MyDecorator, self).decorate()
我必须指定我的类 MyDecorator
的名称作为 super()
的参数。这不是干的。当我现在重命名我的类(class)时,我将不得不重命名它两次。为什么要这样实现?有没有办法避免两次(或多次)写类名?
最佳答案
在 Python 3.0 中,您可以使用 super()
,它等同于 super(ThisClass, self)
。
文档 here .文档中的代码示例:
class C(B):
def method(self, arg):
super().method(arg)
# This does the same thing as: super(C, self).method(arg)
关于python - 为什么我在使用 super() 时必须指定我自己的类,有没有办法绕过它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/466611/