元組對象?

type PyTupleObject?

這個(gè) PyObject 的子類(lèi)型代表一個(gè) Python 的元組對象。

PyTypeObject PyTuple_Type?
Part of the Stable ABI.

PyTypeObject 的實(shí)例代表一個(gè) Python 元組類(lèi)型,這與 Python 層面的 tuple 是相同的對象。

int PyTuple_Check(PyObject *p)?

如果 p 是一個(gè) tuple 對象或者 tuple 類(lèi)型的子類(lèi)型的實(shí)例則返回真值。 此函數總是會(huì )成功執行。

int PyTuple_CheckExact(PyObject *p)?

如果 p 是一個(gè) tuple 對象但不是 tuple 類(lèi)型的子類(lèi)型的實(shí)例則返回真值。 此函數總是會(huì )成功執行。

PyObject *PyTuple_New(Py_ssize_t len)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回一個(gè)新的元組對象,長(cháng)度為 len,失敗時(shí)返回``NULL``。

PyObject *PyTuple_Pack(Py_ssize_t n, ...)?
Return value: New reference. Part of the Stable ABI.

成功時(shí)返回一個(gè)新的元組對象,大小為 n ,失敗時(shí)返回 NULL。 元組值初始化為指向 Python 對象的后續 n 個(gè) C 參數。 PyTuple_Pack(2, a, b)Py_BuildValue("(OO)", a, b) 相等。

Py_ssize_t PyTuple_Size(PyObject *p)?
Part of the Stable ABI.

獲取指向元組對象的指針,并返回該元組的大小。

Py_ssize_t PyTuple_GET_SIZE(PyObject *p)?

返回元組 p 的大小,它必須為非 NULL 并且指向一個(gè)元組;不執行錯誤檢查。

PyObject *PyTuple_GetItem(PyObject *p, Py_ssize_t pos)?
Return value: Borrowed reference. Part of the Stable ABI.

返回 p 所指向的元組中位于 pos 處的對象。 如果 pos 為負值或超出范圍,則返回 NULL 并設置一個(gè) IndexError 異常。

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

類(lèi)似于 PyTuple_GetItem(),但不檢查其參數。

PyObject *PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)?
Return value: New reference. Part of the Stable ABI.

返回 p 所指向的元組的切片,在 lowhigh 之間,或者在失敗時(shí)返回 NULL。 這等同于 Python 表達式 p[low:high]。 不支持從列表末尾索引。

int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)?
Part of the Stable ABI.

p 指向的元組的 pos 位置插入對對象 o 的引用。 成功時(shí)返回 0;如果 pos 越界,則返回 -1,并拋出一個(gè) IndexError 異常。

備注

此函數會(huì )“竊取”對 o 的引用,并丟棄對元組中已在受影響位置的條目的引用。

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

類(lèi)似于 PyTuple_SetItem(),但不進(jìn)行錯誤檢查,并且應該 只是 被用來(lái)填充全新的元組。

備注

This function "steals" a reference to o, and, unlike PyTuple_SetItem(), does not discard a reference to any item that is being replaced; any reference in the tuple at position pos will be leaked.

int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)?

可以用于調整元組的大小。 newsize 將是元組的新長(cháng)度。 因為元組 被認為 是不可變的,所以只有在對象僅有一個(gè)引用時(shí),才應該使用它。 如果元組已經(jīng)被代碼的其他部分所引用,請不要使用此項。 元組在最后總是會(huì )增長(cháng)或縮小。 把它看作是銷(xiāo)毀舊元組并創(chuàng )建一個(gè)新元組,只會(huì )更有效。 成功時(shí)返回 0 。 客戶(hù)端代碼不應假定 *p 的結果值將與調用此函數之前的值相同。 如果替換了 *p 引用的對象,則原始的 *p 將被銷(xiāo)毀。 失敗時(shí),返回``-1``,將 *p 設置為 NULL,并引發(fā) MemoryError 或者 SystemError。

結構序列對象?

結構序列對象是等價(jià)于 namedtuple() 的 C 對象,即一個(gè)序列,其中的條目也可以通過(guò)屬性訪(fǎng)問(wèn)。 要創(chuàng )建結構序列,你首先必須創(chuàng )建特定的結構序列類(lèi)型。

PyTypeObject *PyStructSequence_NewType(PyStructSequence_Desc *desc)?
Return value: New reference. Part of the Stable ABI.

根據 desc 中的數據創(chuàng )建一個(gè)新的結構序列類(lèi)型,如下所述。 可以使用 PyStructSequence_New() 創(chuàng )建結果類(lèi)型的實(shí)例。

void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)?

desc 就地初始化結構序列類(lèi)型 type。

int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)?

PyStructSequence_InitType 相同,但成功時(shí)返回 0 ,失敗時(shí)返回 -1 。

3.4 新版功能.

type PyStructSequence_Desc?
Part of the Stable ABI (including all members).

包含要創(chuàng )建的結構序列類(lèi)型的元信息。

C 類(lèi)型

含意

name

const char *

結構序列類(lèi)型的名稱(chēng)

doc

const char *

指向要忽略類(lèi)型的文檔字符串或 NULL 的指針

fields

PyStructSequence_Field *

指向以 NULL 結尾的數組的指針,其字段名稱(chēng)為新類(lèi)型

n_in_sequence

int

Python側可見(jiàn)的字段數(如果用作元組)

type PyStructSequence_Field?
Part of the Stable ABI (including all members).

描述結構序列的一個(gè)字段。 當結構序列被建模為元組時(shí),所有字段的類(lèi)型都是 PyObject*。 在 PyStructSequence_Descfields 數組中的索引確定了結構序列描述的是哪個(gè)字段。

C 類(lèi)型

含意

name

const char *

字段的名稱(chēng)或 NULL ,若要結束命名字段的列表,請設置為 PyStructSequence_UnnamedField 以保留未命名字段

doc

const char *

要忽略的字段文檔字符串或 NULL

const char *const PyStructSequence_UnnamedField?
Part of the Stable ABI since version 3.11.

字段名的特殊值將保持未命名狀態(tài)。

在 3.9 版更改: 這個(gè)類(lèi)型已從 char * 更改。

PyObject *PyStructSequence_New(PyTypeObject *type)?
Return value: New reference. Part of the Stable ABI.

創(chuàng )建 type 的實(shí)例,該實(shí)例必須使用 PyStructSequence_NewType() 創(chuàng )建。

PyObject *PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)?
Return value: Borrowed reference. Part of the Stable ABI.

返回 p 所指向的結構序列中,位于 pos 處的對象。不需要進(jìn)行邊界檢查。

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

PyStructSequence_GetItem() 的宏版本。

void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)?
Part of the Stable ABI.

將結構序列 p 的索引 pos 處的字段設置為值 o。 與 PyTuple_SET_ITEM() 一樣,它應該只用于填充全新的實(shí)例。

備注

這個(gè)函數“竊取”了指向 o 的一個(gè)引用。

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

Similar to PyStructSequence_SetItem(), but implemented as a static inlined function.

備注

這個(gè)函數“竊取”了指向 o 的一個(gè)引用。