通用对象结构

Python 对象类型定义使用了大量 Structure。此节描述这些 Structure 及如何使用它们。

基对象类型和宏

All Python objects ultimately share a small number of fields at the beginning of the object’s representation in memory. These are represented by the PyObject and PyVarObject types, which are defined, in turn, by the expansions of some macros also used, whether directly or indirectly, in the definition of all other Python objects. Additional macros can be found under reference counting .

type PyObject
属于 Limited API . (Only some members are part of the stable ABI.)

All object types are extensions of this type. This is a type which contains the information Python needs to treat a pointer to an object as an object. In a normal “release” build, it contains only the object’s reference count and a pointer to the corresponding type object. Nothing is actually declared to be a PyObject , but every pointer to a Python object can be cast to a PyObject * . Access to the members must be done by using the macros Py_REFCNT and Py_TYPE .

type PyVarObject
属于 Limited API . (Only some members are part of the stable ABI.)

This is an extension of PyObject that adds the ob_size field. This is only used for objects that have some notion of length . This type does not often appear in the Python/C API. Access to the members must be done by using the macros Py_REFCNT , Py_TYPE ,和 Py_SIZE .

PyObject_HEAD

This is a macro used when declaring new types which represent objects without a varying length. The PyObject_HEAD macro expands to:

PyObject ob_base;