元组对象

PyTupleObject

这个 PyObject 的子类型代表一个 Python 的元组对象。

PyTypeObject PyTuple_Type

PyTypeObject 的实例代表一个 Python 元组类型,这与 Python 层面的 tuple 是相同的对象。

int PyTuple_Check(PyObject *p)

如果 p 是一个元组对象或者元组类型的子类型的实例,则返回真值。

int PyTuple_CheckExact(PyObject *p)

如果 p 是一个元组对象,而不是一个元组子类型的实例,则返回真值。

PyObject* PyTuple_New(Py_ssize_t len)
Return value: New reference.

Return a new tuple object of size len, or NULL on failure.

PyObject* PyTuple_Pack(Py_ssize_t n, ...)
Return value: New reference.

Return a new tuple object of size n, or NULL on failure. The tuple values are initialized to the subsequent n C arguments pointing to Python objects. PyTuple_Pack(2, a, b) is equivalent to Py_BuildValue("(OO)", a, b).

Py_ssize_t PyTuple_Size(PyObject *p)

获取指向元组对象的指针,并返回该元组的大小。

Py_ssize_t PyTuple_GET_SIZE(PyObject *p)

Return the size of the tuple p, which must be non-NULL and point to a tuple; no error checking is performed.

PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)
Return value: Borrowed reference.

Return the object at position pos in the tuple pointed to by p. If pos is out of bounds, return NULL and sets an IndexError exception.

PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)
Return value: Borrowed reference.

类似于 PyTuple_GetItem(),但不检查其参数。

PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)
Return value: New reference.

Take a slice of the tuple pointed to by p from low to high and return it as a new tuple.

int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)

Insert a reference to object o at position pos of the tuple pointed to by p. Return 0 on success.

注解

这个函数“窃取”了指向*o*的一个引用。

void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)

类似于 PyTuple_SetItem(),但不进行错误检查,并且应该 只是 被用来填充全新的元组。

注解

这个函数“窃取”了指向*o*的一个引用。

int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)

Can be used to resize a tuple. newsize will be the new length of the tuple. Because tuples are supposed to be immutable, this should only be used if there is only one reference to the object. Do not use this if the tuple may already be known to some other part of the code. The tuple will always grow or shrink at the end. Think of this as destroying the old tuple and creating a new one, only more efficiently. Returns 0 on success. Client code should never assume that the resulting value of *p will be the same as before calling this function. If the object referenced by *p is replaced, the original *p is destroyed. On failure, returns -1 and sets *p to NULL, and raises MemoryError or SystemError.

int PyTuple_ClearFreeList()

清空释放列表。 返回所释放的条目数。

结构序列对象

结构序列对象是等价于 namedtuple() 的 C 对象,即一个序列,其中的条目也可以通过属性访问。 要创建结构序列,你首先必须创建特定的结构序列类型。

PyTypeObject* PyStructSequence_NewType(PyStructSequence_Desc *desc)

根据 desc 中的数据创建一个新的结构序列类型,如下所述。 可以使用 PyStructSequence_New() 创建结果类型的实例。

void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)

从*desc*就地初始化结构序列类型*type*。

int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)

PyStructSequence_InitType 相同,但成功时返回 0 ,失败时返回 -1

3.4 新版功能.

PyStructSequence_Desc

包含要创建的结构序列类型的元信息。

C 类型

含义

name

char *

结构序列类型的名称

doc

char *

pointer to docstring for the type or NULL to omit

fields

PyStructSequence_Field *

pointer to NULL-terminated array with field names of the new type

n_in_sequence

int

Python侧可见的字段数(如果用作元组)

PyStructSequence_Field

描述一个结构序列的字段。 当结构序列被建模为元组时,所有字段的类型都是 PyObject*PyStructSequence_Descfields 数组的索引确定了描述的是结构序列的哪个字段。

C 类型

含义

name

char *

name for the field or NULL to end the list of named fields, set to PyStructSequence_UnnamedField to leave unnamed

doc

char *

field docstring or NULL to omit

char* PyStructSequence_UnnamedField

字段名的特殊值将保持未命名状态。

PyObject* PyStructSequence_New(PyTypeObject *type)

创建 type 的实例,该实例必须使用 PyStructSequence_NewType() 创建。

PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)

返回*p*所指向的结构序列中,位于*pos*处的对象。不需要进行边界检查。

PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)

PyStructSequence_GetItem() 的宏版本。

void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)

将结构序列 p 的索引 pos 处的字段设置为值 o。 与 PyTuple_SET_ITEM() 一样,它应该只用于填充全新的实例。

注解

这个函数“窃取”了指向*o*的一个引用。

void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)

PyStructSequence_SetItem() 的宏版本。

注解

这个函数“窃取”了指向*o*的一个引用。