列表对象¶
-
PyTypeObject PyList_Type¶
- 属于 稳定 ABI.
这是个属于
PyTypeObject的代表 Python 列表类型的实例。在 Python 层面和类型list是同一个对象。
-
int PyList_Check(PyObject *p)¶
- Thread safety: Atomic.
如果 p 是一个 list 对象或者 list 类型的子类型的实例则返回真值。此函数总是会成功执行。
-
int PyList_CheckExact(PyObject *p)¶
- Thread safety: Atomic.
如果 p 是一个 list 对象但不是 list 类型的子类型的实例则返回真值。此函数总是会成功执行。
-
PyObject *PyList_New(Py_ssize_t len)¶
- 返回值:新的引用。 属于 稳定 ABI.Thread safety: Atomic.
成功时返回一个长度为 len 的新列表,失败时返回
NULL。备注
当 len 大于零时,被返回的列表对象的条目将被设为
NULL。因此你不能使用抽象 API 函数如PySequence_SetItem()或者在使用PyList_SetItem()或PyList_SET_ITEM()将所有条目设为真实对象之前将对象暴露给 Python 代码。以下 API 在该列表完全初始化之前将是安全的 API:PyList_SetItem()和PyList_SET_ITEM()宏。
-
Py_ssize_t PyList_Size(PyObject *list)¶
- 属于 稳定 ABI.Thread safety: Atomic.
返回 list 中列表对象的长度;这等于在列表对象调用
len(list)。
-
Py_ssize_t PyList_GET_SIZE(PyObject *list)¶
- Thread safety: Atomic.
类似于
PyList_Size(),但是不带错误检测。
-
PyObject *PyList_GetItemRef(PyObject *list, Py_ssize_t index)¶
- 返回值:新的引用。 属于 稳定 ABI 自 3.13 版起.Thread safety: Atomic.
返回 list 所指向的列表中 index 位置上的对象。位置值必须为非负数;不支持从列表末尾反向索引。如果 index 超出范围 (
<0 or >=len(list)),则返回NULL并设置IndexError异常。Added in version 3.13.
-
PyObject *PyList_GetItem(PyObject *list, Py_ssize_t index)¶
- 返回值:借入的引用。 属于 稳定 ABI.Thread safety: Safe to call from multiple threads with external synchronization only.
类似于
PyList_GetItemRef(),但返回一个 borrowed reference 而不是 strong reference 引用。备注
In the free-threaded build, the returned borrowed reference may become invalid if another thread modifies the list concurrently. Prefer
PyList_GetItemRef(), which returns a strong reference.
-
PyObject *PyList_GET_ITEM(PyObject *list, Py_ssize_t i)¶
- 返回值:借入的引用。Thread safety: Safe to call from multiple threads with external synchronization only.
类似于
PyList_GetItem(),但是不带错误检测。备注
In the free-threaded build, the returned borrowed reference may become invalid if another thread modifies the list concurrently. Prefer
PyList_GetItemRef(), which returns a strong reference.
-
int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)¶
- 属于 稳定 ABI.Thread safety: Atomic.
将列表中索引为 index 的项设为 item。成功时返回
0。如果 index 超出范围则返回-1并设定IndexError异常。备注
此函数会“偷走”一个对 item 的引用并丢弃一个对列表中受影响位置上的已有条目的引用。
-
void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)¶
- Thread safety: Safe to call from multiple threads with external synchronization only.
PyList_SetItem()的不带错误检测的宏版本。这通常只被用于填充之前没有内容的新列表。当 Python 以 调试模式 或
启用断言构建时将把边界检测作为断言来执行。备注
该宏会“偷走”一个对 item 的引用,但与
PyList_SetItem()不同的是它 不会 丢弃对任何被替换条目的引用;在 list 的 i 位置上的任何引用都将被泄露。备注
In the free-threaded build, this macro has no internal synchronization. It is normally only used to fill in new lists where no other thread has a reference to the list. If the list may be shared, use
PyList_SetItem()instead, which uses a per-object lock.
-
int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)¶
- 属于 稳定 ABI.Thread safety: Safe for concurrent use on the same object.
将条目 item 插入到列表 list 索引号 index 之前的位置。如果成功将返回
0;如果不成功则返回-1并设置一个异常。相当于list.insert(index, item)。
-
int PyList_Append(PyObject *list, PyObject *item)¶
- 属于 稳定 ABI.Thread safety: Atomic.
将对象 item 添加到列表 list 的末尾。如果成功将返回
0;如果不成功则返回-1并设置一个异常。相当于list.append(item)。
-
PyObject *PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)¶
- 返回值:新的引用。 属于 稳定 ABI.Thread safety: Atomic.
返回一个对象列表,包含 list 当中位于 low 和 high 之间 的对象。如果不成功则返回
NULL并设置异常。相当于list[low:high]。不支持从列表末尾进行索引。
-
int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)¶
- 属于 稳定 ABI.Thread safety: Safe for concurrent use on the same object.
将 list 当中 low 与 high 之间的切片设为 itemlist 的内容。相当于
list[low:high] = itemlist。itemlist 可以为NULL,表示赋值为一个空列表(删除切片)。成功时返回0,失败时返回-1。这里不支持从列表末尾进行索引。备注
In the free-threaded build, when itemlist is a
list, both list and itemlist are locked for the duration of the operation. For other iterables (orNULL), only list is locked.
-
int PyList_Extend(PyObject *list, PyObject *iterable)¶
- Thread safety: Safe for concurrent use on the same object.
使用 iterable 的内容扩展 list。这与
PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable)相同并与list.extend(iterable)或list += iterable类似。如果 list 不是
list对象则会引发异常并返回-1。成功时返回 0。Added in version 3.13.
备注
In the free-threaded build, when iterable is a
list,set,dict, or dict view, both list and iterable (or its underlying dict) are locked for the duration of the operation. For other iterables, only list is locked; iterable may be concurrently modified by another thread.
-
int PyList_Clear(PyObject *list)¶
- Thread safety: Atomic.
从 list 移除所有条目。这与
PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL)相同并与list.clear()或del list[:]类似。如果 list 不是
list对象则会引发异常并返回-1。成功时返回 0。Added in version 3.13.
-
int PyList_Sort(PyObject *list)¶
- 属于 稳定 ABI.Thread safety: Safe for concurrent use on the same object.
对 list 中的条目进行原地排序。成功时返回
0,失败时返回-1。这等价于list.sort()。备注
In the free-threaded build, element comparison via
__lt__()can execute arbitrary Python code, during which the per-object lock may be temporarily released. For built-in types (str,int,float), the lock is not released during comparison.
-
int PyList_Reverse(PyObject *list)¶
- 属于 稳定 ABI.Thread safety: Safe for concurrent use on the same object.
对 list 中的条目进行原地反转。成功时返回
0,失败时返回-1。这等价于list.reverse()。