我正在使用attrspython 包,结合继承和插槽。我想从派生方法中调用父类的方法。该问题演示如下:
import attr
@attr.s(slots=True)
class Base():
def meth(self):
print("hello")
@attr.s(slots=True)
class Derived(Base):
def meth(self):
super().meth()
print("world")
d = Derived()
d.meth()
Run Code Online (Sandbox Code Playgroud)
我得到:
TypeError: super(type, obj): obj 必须是 type 的实例或子类型
__slots__=()该问题似乎是由 attrs(具有显式工作的未修饰类)、插槽(常规@attr.s修饰类工作)和普通super()调用(工作)的组合触发的super(Derived, self)。
我想了解super()与显式super(Derived, self)版本的行为有何不同,因为文档说它们“做同样的事情”