子类的元类是如何确定的?

发布于 2025-01-09 15:46:22 字数 705 浏览 2 评论 0原文

对于下面的代码:

class TestMeta(type):
    def __init__(cls, classname, bases, dict_):
        print(f'In TestMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class Parent(metaclass=TestMeta):
    pass

class Child(Parent, metaclass=type):
    pass

输出是:

In TestMeta, class <class '__main__.Parent'>
In TestMeta, class <class '__main__.Child'>

在我看来,在创建类 Parent 时,TestMeta.__init__ 会运行,但是为什么在创建类时又运行>Child 我已将其元类更改为 type?继承传递元类时,元类是如何确定的?

For the code below:

class TestMeta(type):
    def __init__(cls, classname, bases, dict_):
        print(f'In TestMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class Parent(metaclass=TestMeta):
    pass

class Child(Parent, metaclass=type):
    pass

The output is:

In TestMeta, class <class '__main__.Parent'>
In TestMeta, class <class '__main__.Child'>

In my opinion, when creating the class Parent, TestMeta.__init__ will run, but why it runs again when creating the class Child which I have changed its metaclass as type? How is the metaclass determined when inheriting and passing a metaclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2025-01-16 15:46:22

来自文档

类定义的适当元类确定为
如下:

  • 如果没有给出基类和显式元类,则<使用代码>type()

  • 如果给出了显式元类,并且它不是 type(),则直接作为元类使用;

  • 如果type()作为显式元类给出,或者定义了基类,然后使用最派生的元类。



从显式指定的元类中选择最派生的元类
元类(如果有)和全部的元类(即type(cls)
指定的基类。最派生的元类是
所有这些候选元类的子类型。

在上面的例子中,第三个项目符号成立,因为 type 被作为显式元类给出,并且存在基类。

根据最派生元类的定义,候选者是:TestMetatype。由于TestMeta继承了type,因此它是最派生的元类,并且确实是Child的元类。

为了演示差异,以下代码展示了我们如何“强制” Child 获取指定的元类而不是继承的元类

class TestMeta(type):
    def __init__(cls, classname, bases, dict_):
        print(f'In TestMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class OtherMeta(TestMeta):
    def __init__(cls, classname, bases, dict_):
        print(f'In OtherMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class Parent(metaclass=TestMeta):
    pass

class Child(Parent, metaclass=OtherMeta):
    pass

: 。

In TestMeta, class <class '__main__.Parent'>
In OtherMeta, class <class '__main__.Child'>

再次注意,根据最派生元类的定义,只有当 OtherMeta 本身是 TestMeta 的子类时,这才有效 删除这个继承关系会报错:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

From the docs:

The appropriate metaclass for a class definition is determined as
follows:

  • if no bases and no explicit metaclass are given, then type() is used;

  • if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass;

  • if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used.

The most derived metaclass is selected from the explicitly specified
metaclass (if any) and the metaclasses (i.e. type(cls)) of all
specified base classes. The most derived metaclass is one which is a
subtype of all of these candidate metaclasses.

In your case above, the third bullet holds, as type is given as the explicit metaclass and there are base-classes.

According to the definition of the most derived metaclass, the candidates are: TestMeta and type. Since TestMeta inherits type, it is the most derived metaclass and is indeed the metaclass of Child.

To demonstrate the difference, the following code shows how we can "force" Child to get the specified metaclass instead of the inherited one:

class TestMeta(type):
    def __init__(cls, classname, bases, dict_):
        print(f'In TestMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class OtherMeta(TestMeta):
    def __init__(cls, classname, bases, dict_):
        print(f'In OtherMeta, class {cls}')
        type.__init__(cls, classname, bases, dict_)

class Parent(metaclass=TestMeta):
    pass

class Child(Parent, metaclass=OtherMeta):
    pass

The output would be:

In TestMeta, class <class '__main__.Parent'>
In OtherMeta, class <class '__main__.Child'>

Notice that again, according to the definition of the most derived metaclass, this only works if OtherMeta itself is a subclass of TestMeta. Removing this inheritance relation will give the error:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文