元組(Tuple)物件¶
-
PyTypeObject
PyTuple_Type
¶ PyTypeObject
的实例代表一个 Python 元组类型,这与 Python 层面的tuple
是相同的对象。
-
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 toPy_BuildValue("(OO)", a, b)
.
-
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 raisesMemoryError
orSystemError
.
-
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 Type 含义 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_Desc
的fields
数组的索引确定了描述的是结构序列的哪个字段。域 C Type 含义 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*处的对象。不需要进行边界检查。
-
void
PyStructSequence_SetItem
(PyObject *p, Py_ssize_t pos, PyObject *o)¶ 将结构序列 p 的索引 pos 处的字段设置为值 o。 与
PyTuple_SET_ITEM()
一样,它应该只用于填充全新的实例。備註
这个函数“窃取”了指向*o*的一个引用。
-
PyObject*
PyStructSequence_SET_ITEM
(PyObject *p, Py_ssize_t *pos, PyObject *o)¶ PyStructSequence_SetItem()
的宏版本。備註
这个函数“窃取”了指向*o*的一个引用。