小编Nio*_*bos的帖子

super() 和显式 super(Cl,self) (带有 __slots__ 和 attrs)有什么区别

我正在使用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)版本的行为有何不同,因为文档说它们“做同样的事情”

python super python-3.x python-attrs

4
推荐指数
1
解决办法
411
查看次数

标签 统计

python ×1

python-3.x ×1

python-attrs ×1

super ×1