【发布时间】:2018-02-19 21:39:39
【问题描述】:
我正在使用attrs python 包,结合继承和插槽。我想从派生方法中调用父类的方法。问题演示如下:
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()
我明白了:
TypeError: super(type, obj): obj 必须是类型的实例或子类型
问题似乎是由 attrs(具有显式 __slots__=() 工作的未装饰类)、插槽(常规 @attr.s-装饰类工作)和普通 super() 调用(super(Derived, self) 工作)的组合触发的。
我想了解super() 与显式super(Derived, self) 版本的行为有何不同,因为documentation 表示它们“做同样的事情”
【问题讨论】:
-
此问题已在 attrs 中得到修复:github.com/python-attrs/attrs/pull/226
标签: python python-3.x super python-attrs