小编Yur*_*bas的帖子

Python 基类可以是没有 __mro_entries__ 的对象

今天我发现python对象没有__mro_entries__可以用作基类。

例子:

class Base:
    def __init__(self, *args):
        self.args = args

    def __repr__(self):
        return f'{type(self).__name__}(*{self.args!r})'


class Delivered(Base):
    pass


b = Base()
d = Delivered()


class Foo(b, d):
    pass


print(type(Foo) is Delivered)
print(Foo)
Run Code Online (Sandbox Code Playgroud)
True
Delivered(*('Foo', (Base(*()), Delivered(*())), {'__module__': '__main__', '__qualname__': 'Foo'}))
Run Code Online (Sandbox Code Playgroud)

结果Foo将是类的实例Delivered,并且它不是有效的类型。

我确实理解以下用例,__mro_entries__但是使用对象而不__mro_entries__作为基类的用例是什么。这是Python的一个错误吗?

python python-3.x

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

标签 统计

python ×1

python-3.x ×1