在堆中分配对象

PyObject* _PyObject_New(PyTypeObject *type)
Return value: New reference.
PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
Return value: New reference.

在 2.5 版更改: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.

void _PyObject_Del(PyObject *op)
PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
Return value: Borrowed reference.

为新分配的对象 op 初始化它的类型和引用。返回初始化后的对象。如果 type 声明这个对象参与循环垃圾检测,那么这个对象会被添加进垃圾检测的对象集中。这个对象的其他字段不会被影响。

PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
Return value: Borrowed reference.

它的功能和 PyObject_Init() 一样,并且初始化变量大小的对象的长度。

在 2.5 版更改: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.

TYPE* PyObject_New(TYPE, PyTypeObject *type)
Return value: New reference.

使用C的数据结构类型 TYPE 和Python的类型对象 type 分配一个新的Python对象。Python对象头文件中没有定义的字段不会被初始化;对象的引用计数为1。内存分配大小由type对象中的 tp_basicsize 字段决定。

TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Return value: New reference.

使用C的数据结构类型 TYPE 和Python的类型对象 type 分配一个新的Python对象。Python对象头文件中没有定义的字段不会被初始化。被分配的内存空间预留了 TYPE 结构加 type 对象中 tp_itemsize 字段提供的 size 字段的值。这对于实现类似元组这种能够在构造期决定自己大小的对象是很实用的。将字段的数组嵌入到相同的内存分配中可以减少内存分配的次数,这提高了内存分配的效率。

在 2.5 版更改: This function used an int type for size. This might require changes in your code for properly supporting 64-bit systems.

void PyObject_Del(PyObject *op)

释放由 PyObject_New() 或者 PyObject_NewVar() 分配内存的对象。这通常由对象的type字段定义的 tp_dealloc 处理函数来调用。调用这个函数以后op对象中的字段都不可以被访问,因为原分配的内存空间已不再是一个有效的Python对象。

PyObject* Py_InitModule(char *name, PyMethodDef *methods)
Return value: Borrowed reference.

Create a new module object based on a name and table of functions, returning the new module object.

在 2.3 版更改: Older versions of Python did not support NULL as the value for the methods argument.

PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
Return value: Borrowed reference.

Create a new module object based on a name and table of functions, returning the new module object. If doc is non-NULL, it will be used to define the docstring for the module.

在 2.3 版更改: Older versions of Python did not support NULL as the value for the methods argument.

PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
Return value: Borrowed reference.

Create a new module object based on a name and table of functions, returning the new module object. If doc is non-NULL, it will be used to define the docstring for the module. If self is non-NULL, it will be passed to the functions of the module as their (otherwise NULL) first parameter. (This was added as an experimental feature, and there are no known uses in the current version of Python.) For apiver, the only value which should be passed is defined by the constant PYTHON_API_VERSION.

注解

Most uses of this function should probably be using the Py_InitModule3() instead; only use this if you are sure you need it.

在 2.3 版更改: Older versions of Python did not support NULL as the value for the methods argument.

PyObject _Py_NoneStruct

Object which is visible in Python as None. This should only be accessed using the Py_None macro, which evaluates to a pointer to this object.