對象協(xié)議?

PyObject *Py_NotImplemented?

NotImplemented 單例,用于標記某個操作沒有針對給定類型組合的實現(xiàn)。

Py_RETURN_NOTIMPLEMENTED?

C 函數內部應正確處理 Py_NotImplemented 的返回過程(即增加 NotImplemented 的引用計數并返回之)。

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

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

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

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

注意,在調用 __getattr__()__getattribute__() 方法時發(fā)生的異常將被抑制。若要獲得錯誤報告,請換用 PyObject_GetAttr() 。

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

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

注意,在調用 __getattr__()__getattribute__() 方法并創(chuàng)建一個臨時字符串對象時,異常將被抑制。若要獲得錯誤報告,請換用 PyObject_GetAttrString() 。

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

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

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

從對象 o 中讀取一個名為 attr_name 的屬性。成功時返回屬性值,失敗則返回 NULL。這相當于 Python 表達式 o.attr_name

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

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

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

將對象 o 中名為 attr_name 的屬性值設為 v 。失敗時引發(fā)異常并返回 -1;成功時返 回``0`` 。這相當于 Python 語句 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.

將對象 o 中名為 attr_name 的屬性值設為 v 。失敗時引發(fā)異常并返回 -1;成功時返 回``0`` 。這相當于 Python 語句 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.

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

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

刪除對象 o 中名為 attr_name 的屬性。失敗時返回 -1。這相當于 Python 語句 del o.attr_name

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

刪除對象 o 中名為 attr_name 的屬性。失敗時返回 -1。這相當于 Python 語句 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__ 描述符的獲取函數的一種通用實現(xiàn)。必要時會創(chuàng)建該字典。

3.3 新版功能.

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

__dict__ 描述符設置函數的一種通用實現(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_LTPy_LE 、 Py_EQ 、 Py_NEPy_GTPy_GE 之一,分別對應于``<、``<= 、== 、 != 、>>=。這相當于 Python 表達式 o1 op o2,其中 op 是對應于 opid 的操作符。成功時返回比較值,失敗時返回 NULL。

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

opid 指定的操作比較 o1o2 的值,必須是 Py_LT 、 Py_LEPy_EQPy_NE 、 Py_GTPy_GE 之一,分別對應于 <<=、 == 、!=>>=。錯誤時返回 -1,若結果為 false 則返回 0,否則返回 1。這相當于 Python 表達式 o1 op o2,其中 op 是對應于 opid 的操作符。

備注

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

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

計算對象 o 的字符串形式。 成功時返回字符串,失敗時返回 NULL。 這相當于 Python 表達式 repr(o)。 由內置函數 repr() 調用。

在 3.4 版更改: 該函數現(xiàn)在包含一個調試斷言,用以確保不會靜默地丟棄活動的異常。

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

PyObject_Repr() 一樣,計算對象 o 的字符串形式,但在 PyObject_Repr() 返回的字符串中用 \x、\u\U 轉義非 ASCII 字符。這將生成一個類似于 Python 2 中由 PyObject_Repr() 返回的字符串。由內置函數 ascii() 調用。

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

計算對象 o 的字符串形式。 成功時返回字符串,失敗時返回 NULL。 這相當于 Python 表達式 str(o)。由內置函數 str() 調用,因此也由 print() 函數調用。

在 3.4 版更改: 該函數現(xiàn)在包含一個調試斷言,用以確保不會靜默地丟棄活動的異常。

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

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

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

如果 derived 類與 cls 類相同或為其派生類,則返回 1,否則返回 0。 如果出錯則返回 -1

如果 cls 是元組,則會對 cls 進行逐項檢測。如果至少有一次檢測返回 1,結果將為 1,否則將是 0。

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

通常只有類對象才會被視為類,即 type 或派生類的實例。然而,對象可以通過擁有 __bases__ 屬性(必須是基類的元組)來覆蓋這一點。

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

如果 instcls 類或其子類的實例,則返回 1,如果不是則返回``0``。 如果出錯則返回 -1 并設置一個異常。

如果 cls 是元組,則會對 cls 進行逐項檢測。如果至少有一次檢測返回 1,結果將為 1,否則將是 0

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

實例 inst 可以通過 __class__ 屬性來覆蓋其所屬類。

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

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

計算并返回對象的哈希值 o。 失敗時返回 -1。這相當于 Python 表達式 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.

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

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

如果對象 o 被認為是 true,則返回 1,否則返回 0。這相當于 Python 表達式 not not o。 失敗則返回 -1

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

如果對象 o 被認為是 true,則返回 1,否則返回 0。這相當于 Python 表達式 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)?

如果對象 otype 類型或其子類型,則返回非零,否則返回 0。兩個參數都必須非 NULL。

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

返回對象 o 的長度。 如果對象 o 支持序列和映射協(xié)議,則返回序列長度。 出錯時返回 -1。這等同于 Python 表達式 len(o)。

Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)?

返回對象 o 的估計長度。首先嘗試返回實際長度,然后用 __length_hint__() 進行估計,最后返回默認值。出錯時返回``-1``。這等同于 Python 表達式 operator.length_hint(o, defaultvalue)。

3.4 新版功能.

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

返回對象 key 對應的 o 元素,或在失敗時返回 NULL。這等同于 Python 表達式 o[key]。

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

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

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

從對象 o 中移除對象 key 的映射。失敗時返回 -1。 這相當于 Python 語句 del o[key]

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

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

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

等同于 Python 表達式 iter(o)。為對象參數返回一個新的迭代器,如果該對象已經是一個迭代器,則返回對象本身。如果對象不能被迭代,會引發(fā) TypeError ,并返回 NULL。

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

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

3.10 新版功能.