Cpython如何实现其类型对象,即类型的类型始终是类型? [英] how does Cpython implement its type Objects, i.e. type's type is always type?

查看:171
本文介绍了Cpython如何实现其类型对象,即类型的类型始终是类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解python中的所有内容都是对象,而这些对象的类型(或类)是类型。加类型的类型也是类型本身。 (如此处很好地解释)

I understand that everything in python is an Object and that the 'type' (or class) of these object is 'type'. Plus the type of type is also type itself. (as explained nicely here)

我不知道该循环引用如何实施?因此,我在此处进行了查找。引用可能解释我正在寻找的部分的内容:

What I do not understand is how is this circular reference implemented? So i looked here. To quote the portion which might explain what I am looking for:

PyTypeObject* PyObject.ob_type

这是类型的类型,即它的元类型。它由PyObject_HEAD_INIT宏的参数初始化,其值通常应为& PyType_Type。但是,对于必须在Windows上使用(至少)的动态可加载扩展模块,编译器会抱怨这不是有效的初始化程序。因此,惯例是将NULL传递给PyObject_HEAD_INIT宏,并在执行其他任何操作之前,在模块初始化函数的开头显式初始化此字段。通常是这样完成的:

Foo_Type.ob_type = &PyType_Type;

由于C不是基于OOP的,所以我知道当创建一个类时,它可以具有一个属性指向对象本身作为自己的类。我敢肯定,我在这里的缺乏理解使我陷入混乱的状态,如果有人可以对此有所了解,谁能指出这是否是其他脚本语言或某种模式设计中的常规做法,我会为此感激的。

Since C is not OOP based, I understand that when one creates a class, it can have an attribute to point to the object itself as its own class. I am sure that my lack of understanding here has led me in a state of confusion, can anyone point out if this is a usual practice in design of other scripting languages or some kind of a pattern, if anyone can throw some light on this, I will be grateful for it.

编辑:我发现了此处

PyObject* PyType_Type

这是类型对象的类型对象;

那是怎么回事?

推荐答案

定义PyType_Type.ob_type =& PyType_Type的代码涉及两个间接寻址。当它调用 PyType_Ready(& PyType_Type)时,它们都从函数 _Py_ReadyTypes()开始。在调用该函数之前,成员 tp_base ob_type 都为 NULL 。该函数首先将 type-> tp_base 设置为& PyBaseObject_Type (即 object 在Python空间中),然后设置 type-> ob_type = PyBaseObject_Type.ob_type 。该代码使用 Py_TYPE(),它只是 ob-> ob_type 的宏。由于对象的类型是类型,因此代码将类型设置为类型。

The code that defines PyType_Type.ob_type = &PyType_Type involves a couple of indirections. It all starts in the function _Py_ReadyTypes() when it calls PyType_Ready(&PyType_Type). Before the function is called, the members tp_base and ob_type are both NULL. The function first sets type->tp_base to &PyBaseObject_Type (which is object in Python space) and then sets type->ob_type = PyBaseObject_Type.ob_type. The code uses Py_TYPE() which is just a macro for ob->ob_type. Since the type of object is type the code sets the type of type to type.

现在您拥有:

>>> type.__bases__
(<class 'object'>,)
>>> type(object)
<class 'type'>
>>> type(type(object))
<class 'type'>

定义使类型成为对象的实例加自身,而对象成为类型的实例。

The definition makes type an instance of object plus itself and object an instance of type.

>>> isinstance(type, object)
True
>>> isinstance(object, type)
True
>>> isinstance(type, type)
True

类型初始化代码在以下代码中更容易理解Python伪代码:

The type initialization code is much easier to understand in Python pseudo-code:

# object's class is type
object.__class__ = type
# PyType_Ready(type) sets:
type.__bases__ = (object,)
type.__class__ = type(object)

这篇关于Cpython如何实现其类型对象,即类型的类型始终是类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆