對(duì)象協(xié)議?
-
PyObject *Py_NotImplemented?
NotImplemented單例,用于標(biāo)記某個(gè)操作沒(méi)有針對(duì)給定類(lèi)型組合的實(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 寫(xiě)入到文件 fp。 出錯(cuò)時(shí)返回
-1。 旗標(biāo)參數(shù)被用于啟用特定的輸出選項(xiàng)。 目前唯一支持的選項(xiàng)是Py_PRINT_RAW;如果給出該選項(xiàng),則將寫(xiě)入對(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ù),用于放入類(lèi)型對(duì)象的
tp_getattro槽中。它在類(lèi)的字典中(位于對(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 usingPyObject_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 usingPyObject_DelAttrString().
-
int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)?
- Part of the Stable ABI.
通用的屬性設(shè)置和刪除函數(shù),用于放入類(lèi)型對(duì)象的
tp_setattro槽。它在類(lèi)的字典中(位于對(duì)象的MRO中)查找數(shù)據(jù)描述器,如果找到,則將比在實(shí)例字典中設(shè)置或刪除屬性?xún)?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 指定的操作比較 o1 和 o2 的值,必須是
Py_LT、Py_LE、Py_EQ、Py_NE、Py_GT或Py_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 指定的操作比較 o1 和 o2 的值,必須是
Py_LT、Py_LE、Py_EQ、Py_NE、Py_GT或Py_GE之一,分別對(duì)應(yīng)于<、<=、==、!=、>或>=。錯(cuò)誤時(shí)返回-1,若結(jié)果為 false 則返回0,否則返回1。這相當(dāng)于 Python 表達(dá)式o1 op o2,其中op是對(duì)應(yīng)于 opid 的操作符。
備注
如果 o1 和 o2 是同一個(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è)類(lèi)似于 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 類(lèi)與 cls 類(lèi)相同或?yàn)槠渑缮?lèi),則返回
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)用以確定子類(lèi)的狀態(tài)。 否則,如果 derived 是個(gè)直接或間接子類(lèi),即包含在cls.__mro__中,那么它就是 cls 的一個(gè)子類(lèi)。通常只有類(lèi)對(duì)象才會(huì)被視為類(lèi),即
type或派生類(lèi)的實(shí)例。然而,對(duì)象可以通過(guò)擁有__bases__屬性(必須是基類(lèi)的元組)來(lái)覆蓋這一點(diǎn)。
-
int PyObject_IsInstance(PyObject *inst, PyObject *cls)?
- Part of the Stable ABI.
如果 inst 是 cls 類(lèi)或其子類(lèi)的實(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)用以確定子類(lèi)的狀態(tài)。 否則,如果 derived 是 cls 的子類(lèi),那么它就是 cls 的一個(gè)實(shí)例。實(shí)例 inst 可以通過(guò)
__class__屬性來(lái)覆蓋其所屬類(lèi)。對(duì)象 cls 是否被認(rèn)作類(lèi),以及基類(lèi)是什么,均可通過(guò)
__bases__屬性(必須是基類(lèi)的元組)進(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è)類(lèi)型向解釋器顯式表明它不可散列。
-
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, raisesSystemErrorand returnsNULL. This is equivalent to the Python expressiontype(o). This function increments the reference count of the return value. There's really no reason to use this function instead of thePy_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ì)象 o 是 type 類(lèi)型或其子類(lèi)型,則返回非零,否則返回
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,類(lèi)似 Python 的dir(),則返回當(dāng)前 locals 的名字;這時(shí)如果沒(méi)有活動(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 新版功能.