在堆上分配对象¶
-
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.
-
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 对象标头中定义的字段不会被初始化;对象的引用计数将为一。 内存分配大小由 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.