對(duì)象協(xié)議?

PyObject *Py_NotImplemented?

NotImplemented 單例,用于標(biāo)記某個(gè)操作沒有針對(duì)給定類型組合的實(shí)現(xiàn)。

Py_RETURN_NOTIMPLEMENTED?

C 函數(shù)內(nèi)部應(yīng)正確處理 Py_NotImplemented 的返回過(guò)程(即增加 NotImplemented 的引用計(jì)數(shù)并返回之)。

int PyObject_Print(PyObject *o, FILE *fp, int flags)?

將對(duì)象 o 寫入到文件 fp。 出錯(cuò)時(shí)返回 -1 。 旗標(biāo)參數(shù)被用于啟用特定的輸出選項(xiàng)。 目前唯一支持的選項(xiàng)是 Py_PRINT_RAW;如果給出該選項(xiàng),則將寫入對(duì)象的 str() 而不是 repr()。

int PyObject_HasAttr(PyObject *o, PyObject *attr_name)?
Part of the Stable ABI.

如果 o 帶有屬性 attr_name,則返回 1,否則返回 0。這相當(dāng)于 Python 表達(dá)式 hasattr(o, attr_name)。 此函數(shù)總是成功。

注意,在調(diào)用 __getattr__()__getattribute__() 方法時(shí)發(fā)生的異常將被抑制。若要獲得錯(cuò)誤報(bào)告,請(qǐng)換用 PyObject_GetAttr() 。

int PyObject_HasAttrString(PyObject *o, const char *attr_name)?
Part of the Stable ABI.

如果 o 帶有屬性 attr_name,則返回 1,否則返回 0。這相當(dāng)于 Python 表達(dá)式 hasattr(o, attr_name)。 此函數(shù)總是成功。

注意,在調(diào)用 __getattr__()__getattribute__() 方法并創(chuàng)建一個(gè)臨時(shí)字符串對(duì)象時(shí),異常將被抑制。若要獲得錯(cuò)誤報(bào)告,請(qǐng)換用 PyObject_GetAttrString() 。

PyObject *PyObject_GetAttr(PyObject *o, PyObject *attr_name)?
Return value: New reference. Part of the Stable ABI.

從對(duì)象 o 中讀取名為 attr_name 的屬性。成功返回屬性值,失敗則返回 NULL。 這相當(dāng)于 Python 表達(dá)式 o.attr_name。

PyObject *PyObject_GetAttrString(PyObject *o, const char *attr_name)?
Return value: New reference. Part of the Stable ABI.

從對(duì)象 o 中讀取一個(gè)名為 attr_name 的屬性。成功時(shí)返回屬性值,失敗則返回 NULL。這相當(dāng)于 Python 表達(dá)式 o.attr_name。

PyObject *PyObject_GenericGetAttr(PyObject *o, PyObject *name)?
Return value: New reference. Part of the Stable ABI.

通用的屬性獲取函數(shù),用于放入類型對(duì)象的 tp_getattro 槽中。它在類的字典中(位于對(duì)象的 MRO 中)查找某個(gè)描述符,并在對(duì)象的 __dict__ 中查找某個(gè)屬性。正如 實(shí)現(xiàn)描述器 所述,數(shù)據(jù)描述符優(yōu)先于實(shí)例屬性,而非數(shù)據(jù)描述符則不優(yōu)先。失敗則會(huì)觸發(fā) AttributeError 。

int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)?
Part of the Stable ABI.

將對(duì)象 o 中名為 attr_name 的屬性值設(shè)為 v 。失敗時(shí)引發(fā)異常并返回 -1;成功時(shí)返 回``0`` 。這相當(dāng)于 Python 語(yǔ)句 o.attr_name = v。

If v is NULL, the attribute is deleted. This behaviour is deprecated in favour of using PyObject_DelAttr(), but there are currently no plans to remove it.

int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)?
Part of the Stable ABI.

將對(duì)象 o 中名為 attr_name 的屬性值設(shè)為 v 。失敗時(shí)引發(fā)異常并返回 -1;成功時(shí)返 回``0`` 。這相當(dāng)于 Python 語(yǔ)句 o.attr_name = v。

If v is NULL, the attribute is deleted, but this feature is deprecated in favour of using PyObject_DelAttrString().

int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)?
Part of the Stable ABI.

通用的屬性設(shè)置和刪除函數(shù),用于放入類型對(duì)象的 tp_setattro 槽。它在類的字典中(位于對(duì)象的MRO中)查找數(shù)據(jù)描述器,如果找到,則將比在實(shí)例字典中設(shè)置或刪除屬性優(yōu)先執(zhí)行。否則,該屬性將在對(duì)象的 __dict__ 中設(shè)置或刪除。如果成功將返回 0,否則將引發(fā) AttributeError 并返回 -1。

int PyObject_DelAttr(PyObject *o, PyObject *attr_name)?

刪除對(duì)象 o 中名為 attr_name 的屬性。失敗時(shí)返回 -1。這相當(dāng)于 Python 語(yǔ)句 del o.attr_name。

int PyObject_DelAttrString(PyObject *o, const char *attr_name)?

刪除對(duì)象 o 中名為 attr_name 的屬性。失敗時(shí)返回 -1。這相當(dāng)于 Python 語(yǔ)句 del o.attr_name。

PyObject *PyObject_GenericGetDict(PyObject *o, void *context)?
Return value: New reference. Part of the Stable ABI since version 3.10.

__dict__ 描述符的獲取函數(shù)的一種通用實(shí)現(xiàn)。必要時(shí)會(huì)創(chuàng)建該字典。

3.3 新版功能.

int PyObject_GenericSetDict(PyObject *o, PyObject *value, void *context)?
Part of the Stable ABI since version 3.7.

__dict__ 描述符設(shè)置函數(shù)的一種通用實(shí)現(xiàn)。這里不允許刪除該字典。

3.3 新版功能.

PyObject *PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)?
Return value: New reference. Part of the Stable ABI.

opid 指定的操作比較 o1o2 的值,必須是 Py_LT 、 Py_LE 、 Py_EQPy_NE 、 Py_GTPy_GE 之一,分別對(duì)應(yīng)于``<、``<= 、== 、 !=>>=。這相當(dāng)于 Python 表達(dá)式 o1 op o2,其中 op 是對(duì)應(yīng)于 opid 的操作符。成功時(shí)返回比較值,失敗時(shí)返回 NULL。

int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)?
Part of the Stable ABI.

opid 指定的操作比較 o1o2 的值,必須是 Py_LT 、 Py_LE 、 Py_EQ 、 Py_NEPy_GTPy_GE 之一,分別對(duì)應(yīng)于 < 、<=、 == 、!= 、>>=。錯(cuò)誤時(shí)返回 -1,若結(jié)果為 false 則返回 0,否則返回 1。這相當(dāng)于 Python 表達(dá)式 o1 op o2,其中 op 是對(duì)應(yīng)于 opid 的操作符。

備注

如果 o1o2 是同一個(gè)對(duì)象,PyObject_RichCompareBool()Py_EQ 則返回 1 ,為 Py_NE 則返回 0。

PyObject *PyObject_Repr(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

計(jì)算對(duì)象 o 的字符串形式。 成功時(shí)返回字符串,失敗時(shí)返回 NULL。 這相當(dāng)于 Python 表達(dá)式 repr(o)。 由內(nèi)置函數(shù) repr() 調(diào)用。

在 3.4 版更改: 該函數(shù)現(xiàn)在包含一個(gè)調(diào)試斷言,用以確保不會(huì)靜默地丟棄活動(dòng)的異常。

PyObject *PyObject_ASCII(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

PyObject_Repr() 一樣,計(jì)算對(duì)象 o 的字符串形式,但在 PyObject_Repr() 返回的字符串中用 \x\u\U 轉(zhuǎn)義非 ASCII 字符。這將生成一個(gè)類似于 Python 2 中由 PyObject_Repr() 返回的字符串。由內(nèi)置函數(shù) ascii() 調(diào)用。

PyObject *PyObject_Str(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

計(jì)算對(duì)象 o 的字符串形式。 成功時(shí)返回字符串,失敗時(shí)返回 NULL。 這相當(dāng)于 Python 表達(dá)式 str(o)。由內(nèi)置函數(shù) str() 調(diào)用,因此也由 print() 函數(shù)調(diào)用。

在 3.4 版更改: 該函數(shù)現(xiàn)在包含一個(gè)調(diào)試斷言,用以確保不會(huì)靜默地丟棄活動(dòng)的異常。

PyObject *PyObject_Bytes(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

計(jì)算對(duì)象 o 的字節(jié)形式。失敗時(shí)返回 NULL,成功時(shí)返回一個(gè)字節(jié)串對(duì)象。這相當(dāng)于 o 不是整數(shù)時(shí)的 Python 表達(dá)式 bytes(o) 。與 bytes(o) 不同的是,當(dāng) o 是整數(shù)而不是初始為 0 的字節(jié)串對(duì)象時(shí),會(huì)觸發(fā) TypeError。

int PyObject_IsSubclass(PyObject *derived, PyObject *cls)?
Part of the Stable ABI.

如果 derived 類與 cls 類相同或?yàn)槠渑缮?,則返回 1,否則返回 0。 如果出錯(cuò)則返回 -1

如果 cls 是元組,則會(huì)對(duì) cls 進(jìn)行逐項(xiàng)檢測(cè)。如果至少有一次檢測(cè)返回 1,結(jié)果將為 1,否則將是 0。

正如 PEP 3119 所述,如果 cls 帶有 __subclasscheck__() 方法,將會(huì)被調(diào)用以確定子類的狀態(tài)。 否則,如果 derived 是個(gè)直接或間接子類,即包含在 cls.__mro__ 中,那么它就是 cls 的一個(gè)子類。

通常只有類對(duì)象才會(huì)被視為類,即 type 或派生類的實(shí)例。然而,對(duì)象可以通過(guò)擁有 __bases__ 屬性(必須是基類的元組)來(lái)覆蓋這一點(diǎn)。

int PyObject_IsInstance(PyObject *inst, PyObject *cls)?
Part of the Stable ABI.

如果 instcls 類或其子類的實(shí)例,則返回 1,如果不是則返回``0``。 如果出錯(cuò)則返回 -1 并設(shè)置一個(gè)異常。

如果 cls 是元組,則會(huì)對(duì) cls 進(jìn)行逐項(xiàng)檢測(cè)。如果至少有一次檢測(cè)返回 1,結(jié)果將為 1,否則將是 0。

正如 PEP 3119 所述,如果 cls 帶有 __subclasscheck__() 方法,將會(huì)被調(diào)用以確定子類的狀態(tài)。 否則,如果 derivedcls 的子類,那么它就是 cls 的一個(gè)實(shí)例。

實(shí)例 inst 可以通過(guò) __class__ 屬性來(lái)覆蓋其所屬類。

對(duì)象 cls 是否被認(rèn)作類,以及基類是什么,均可通過(guò) __bases__ 屬性(必須是基類的元組)進(jìn)行覆蓋。

Py_hash_t PyObject_Hash(PyObject *o)?
Part of the Stable ABI.

計(jì)算并返回對(duì)象的哈希值 o。 失敗時(shí)返回 -1。這相當(dāng)于 Python 表達(dá)式 hash(o)。

在 3.2 版更改: The return type is now Py_hash_t. This is a signed integer the same size as Py_ssize_t.

Py_hash_t PyObject_HashNotImplemented(PyObject *o)?
Part of the Stable ABI.

設(shè)置一個(gè) TypeError 表示 type(o) 是不可哈希的,并返回 -1 。該函數(shù)保存在 tp_hash 槽中時(shí)會(huì)受到特別對(duì)待,允許某個(gè)類型向解釋器顯式表明它不可散列。

int PyObject_IsTrue(PyObject *o)?
Part of the Stable ABI.

如果對(duì)象 o 被認(rèn)為是 true,則返回 1,否則返回 0。這相當(dāng)于 Python 表達(dá)式 not not o。 失敗則返回 -1。

int PyObject_Not(PyObject *o)?
Part of the Stable ABI.

如果對(duì)象 o 被認(rèn)為是 true,則返回 1,否則返回 0。這相當(dāng)于 Python 表達(dá)式 not not o。 失敗則返回 -1。

PyObject *PyObject_Type(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

When o is non-NULL, returns a type object corresponding to the object type of object o. On failure, raises SystemError and returns NULL. This is equivalent to the Python expression type(o). This function increments the reference count of the return value. There's really no reason to use this function instead of the Py_TYPE() function, which returns a pointer of type PyTypeObject*, except when the incremented reference count is needed.

int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)?

如果對(duì)象 otype 類型或其子類型,則返回非零,否則返回 0。兩個(gè)參數(shù)都必須非 NULL。

Py_ssize_t PyObject_Size(PyObject *o)?
Py_ssize_t PyObject_Length(PyObject *o)?
Part of the Stable ABI.

返回對(duì)象 o 的長(zhǎng)度。 如果對(duì)象 o 支持序列和映射協(xié)議,則返回序列長(zhǎng)度。 出錯(cuò)時(shí)返回 -1。這等同于 Python 表達(dá)式 len(o)。

Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)?

返回對(duì)象 o 的估計(jì)長(zhǎng)度。首先嘗試返回實(shí)際長(zhǎng)度,然后用 __length_hint__() 進(jìn)行估計(jì),最后返回默認(rèn)值。出錯(cuò)時(shí)返回``-1``。這等同于 Python 表達(dá)式 operator.length_hint(o, defaultvalue)。

3.4 新版功能.

PyObject *PyObject_GetItem(PyObject *o, PyObject *key)?
Return value: New reference. Part of the Stable ABI.

返回對(duì)象 key 對(duì)應(yīng)的 o 元素,或在失敗時(shí)返回 NULL。這等同于 Python 表達(dá)式 o[key]。

int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)?
Part of the Stable ABI.

將對(duì)象 key 映射到值 v。 失敗時(shí)引發(fā)異常并返回 -1;成功時(shí)返回 0。 這相當(dāng)于 Python 語(yǔ)句 o[key] = v。該函數(shù) 不會(huì) 偷取 v 的引用計(jì)數(shù)。

int PyObject_DelItem(PyObject *o, PyObject *key)?
Part of the Stable ABI.

從對(duì)象 o 中移除對(duì)象 key 的映射。失敗時(shí)返回 -1。 這相當(dāng)于 Python 語(yǔ)句 del o[key]。

PyObject *PyObject_Dir(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

相當(dāng)于 Python 表達(dá)式 dir(o),返回一個(gè)(可能為空)適合對(duì)象參數(shù)的字符串列表,如果出錯(cuò)則返回 NULL。 如果參數(shù)為 NULL,類似 Python 的 dir(),則返回當(dāng)前 locals 的名字;這時(shí)如果沒有活動(dòng)的執(zhí)行框架,則返回 NULL,但 PyErr_Occurred() 將返回 false。

PyObject *PyObject_GetIter(PyObject *o)?
Return value: New reference. Part of the Stable ABI.

等同于 Python 表達(dá)式 iter(o)。為對(duì)象參數(shù)返回一個(gè)新的迭代器,如果該對(duì)象已經(jīng)是一個(gè)迭代器,則返回對(duì)象本身。如果對(duì)象不能被迭代,會(huì)引發(fā) TypeError ,并返回 NULL

PyObject *PyObject_GetAIter(PyObject *o)?
Return value: New reference. Part of the Stable ABI since version 3.10.

等同于 Python 表達(dá)式 aiter(o)。接受一個(gè) AsyncIterable 對(duì)象,并為其返回一個(gè) AsyncIterator。通常返回的是一個(gè)新迭代器,但如果參數(shù)是一個(gè) AsyncIterator,將返回其自身。如果該對(duì)象不能被迭代,會(huì)引發(fā) TypeError,并返回 NULL。

3.10 新版功能.