引用计数

本节介绍的宏被用于管理 Python 对象的引用计数。

void Py_INCREF(PyObject *o)

Indicate taking a new strong reference to object o, indicating it is in use and should not be destroyed.

此函数通常被用来将 borrowed reference 原地转换为 strong referencePy_NewRef() 函数可被用来创建新的 strong reference

When done using the object, release it by calling Py_DECREF().

此对象必须不为 NULL;如果你不能确定它不为 NULL,请使用 Py_XINCREF()

Do not expect this function to actually modify o in any way.

void Py_XINCREF(PyObject *o)

Similar to Py_INCREF(), but the object o can be NULL, in which case this has no effect.

另请参阅 Py_XNewRef()

PyObject *Py_NewRef(PyObject *o)
Part of the Stable ABI since version 3.10.

Create a new strong reference to an object: call Py_INCREF() on o and return the object o.

When the strong reference is no longer needed, Py_DECREF() should be called on it to release the reference.

对象 o 必须不为 NULL;如果 o 可以为 NULL 则应改用 Py_XNewRef()

例如:

Py_INCREF(obj);
self->attr = obj;

可以写成:

self->attr = Py_NewRef(obj);

另请参阅 Py_INCREF()

3.10 新版功能.

PyObject *Py_XNewRef(PyObject *o)
Part of the Stable ABI since version 3.10.

类似于 Py_NewRef(),但对象 o 可以为 NULL。

如果对象 oNULL,该函数也·将返回 NULL

3.10 新版功能.

void Py_DECREF(PyObject *o)

Release a strong reference to object o, indicating the reference is no longer used.

Once the last strong reference is released (i.e. the object's reference count reaches 0), the object's type's deallocation function (which must not be NULL) is invoked.

此函数通常被用于在退出作用域之前删除一个 strong reference

此对象必须不为 NULL;如果你不能确定它不为 NULL,请使用 Py_XDECREF()

Do not expect this function to actually modify o in any way.

警告

释放函数会导致任意 Python 代码被发起调用(例如当一个带有 __del__() 方法的类实例被释放时就是如此)。 虽然这些代码中的异常不会被传播,但被执行的代码能够自由访问所有 Python 全局变量。 这意味着在调用 Py_DECREF() 之前任何可通过全局变量获取的对象都应该处于完好的状态。 例如,从一个列表中删除对象的代码应该将被删除对象的引用拷贝到一个临时变量中,更新列表数据结构,然后再为临时变量调用 Py_DECREF()

void Py_XDECREF(PyObject *o)

Similar to Py_DECREF(), but the object o can be NULL, in which case this has no effect. The same warning from Py_DECREF() applies here as well.

void Py_CLEAR(PyObject *o)

Release a strong reference for object o. The object may be NULL, in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF(), except that the argument is also set to NULL. The warning for Py_DECREF() does not apply with respect to the object passed because the macro carefully uses a temporary variable and sets the argument to NULL before releasing the reference.

It is a good idea to use this macro whenever releasing a reference to an object that might be traversed during garbage collection.

void Py_IncRef(PyObject *o)
Part of the Stable ABI.

Indicate taking a new strong reference to object o. A function version of Py_XINCREF(). It can be used for runtime dynamic embedding of Python.

void Py_DecRef(PyObject *o)
Part of the Stable ABI.

Release a strong reference to object o. A function version of Py_XDECREF(). It can be used for runtime dynamic embedding of Python.

以下函数或宏仅可在解释器核心内部使用: _Py_Dealloc(), _Py_ForgetReference(), _Py_NewReference() 以及全局变量 _Py_RefTotal