對象協議?
-
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)? 如果 o 帶有屬性 attr_name,則返回
1,否則返回0。這相當于 Python 表達式hasattr(o, attr_name)。 此函數總是成功。注意,在調用
__getattr__()和__getattribute__()方法時發生的異常將被抑制。若要獲得錯誤報告,請換用PyObject_GetAttr()。
-
int
PyObject_HasAttrString(PyObject?*o, const char?*attr_name)? 如果 o 帶有屬性 attr_name,則返回
1,否則返回0。這相當于 Python 表達式hasattr(o, attr_name)。 此函數總是成功。注意,在調用
__getattr__()和__getattribute__()方法并創建一個臨時字符串對象時,異常將被抑制。若要獲得錯誤報告,請換用PyObject_GetAttrString()。
-
PyObject*
PyObject_GetAttr(PyObject?*o, PyObject?*attr_name)? - Return value: New reference.
從對象 o 中讀取名為 attr_name 的屬性。成功返回屬性值,失敗則返回
NULL。 這相當于 Python 表達式o.attr_name。
-
PyObject*
PyObject_GetAttrString(PyObject?*o, const char?*attr_name)? - Return value: New reference.
從對象 o 中讀取一個名為 attr_name 的屬性。成功時返回屬性值,失敗則返回
NULL。這相當于 Python 表達式o.attr_name。
-
PyObject*
PyObject_GenericGetAttr(PyObject?*o, PyObject?*name)? - Return value: New reference.
通用的屬性獲取函數,用于放入類型對象的
tp_getattro槽中。它在類的字典中(位于對象的 MRO 中)查找某個描述符,并在對象的__dict__中查找某個屬性。正如 實現描述器 所述,數據描述符優先于實例屬性,而非數據描述符則不優先。失敗則會觸發AttributeError。
-
int
PyObject_SetAttr(PyObject?*o, PyObject?*attr_name, PyObject?*v)? 將對象 o 中名為 attr_name 的屬性值設為 v 。失敗時引發異常并返回
-1;成功時返 回``0`` 。這相當于 Python 語句o.attr_name = v。如果 v 為
NULL,屬性將被刪除,但是此功能已被棄用,應改用PySequence_DelItem()。
-
int
PyObject_SetAttrString(PyObject?*o, const char?*attr_name, PyObject?*v)? 將對象 o 中名為 attr_name 的屬性值設為 v 。失敗時引發異常并返回
-1;成功時返 回``0`` 。這相當于 Python 語句o.attr_name = v。如果 v 為
NULL,該屬性將被刪除,但是此功能已被棄用,應改用PySequence_DelItem()。
-
int
PyObject_GenericSetAttr(PyObject?*o, PyObject?*name, PyObject?*value)? 通用的屬性設置和刪除函數,用于放入類型對象的
tp_setattro槽。它在類的字典中(位于對象的MRO中)查找數據描述器,如果找到,則將比在實例字典中設置或刪除屬性優先執行。否則,該屬性將在對象的__dict__中設置或刪除。如果成功將返回0,否則將引發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.
__dict__描述符的獲取函數的一種通用實現。必要時會創建字典。3.3 新版功能.
-
int
PyObject_GenericSetDict(PyObject?*o, PyObject?*value, void?*context)? __dict__描述符設置函數的一種通用實現。這里不允許刪除字典。3.3 新版功能.
-
PyObject*
PyObject_RichCompare(PyObject?*o1, PyObject?*o2, int?opid)? - Return value: New reference.
用 opid 指定的操作比較 o1 和 o2 的值,必須是
Py_LT、Py_LE、Py_EQ、Py_NE、Py_GT或Py_GE之一,分別對應于``<=`` 、==、!=、>或>=。這相當于 Python 表達式o1 op o2,其中op是對應于 opid 的操作符。成功時返回比較值,失敗時返回NULL。
-
int
PyObject_RichCompareBool(PyObject?*o1, PyObject?*o2, int?opid)? 用 opid 指定的操作比較 o1 和 o2 的值,必須是
Py_LT、Py_LE、Py_EQ、Py_NE、Py_GT或Py_GE之一,分別對應于<、<=、==、!=、>或>=。錯誤時返回-1,若結果為 false 則返回0,否則返回1。這相當于 Python 表達式o1 op o2,其中op是對應于 opid 的操作符。
注解
如果 o1 和 o2 是同一個對象,PyObject_RichCompareBool() 為 Py_EQ 則返回 1 ,為 Py_NE 則返回 0。
-
PyObject*
PyObject_Repr(PyObject?*o)? - Return value: New reference.
計算對象 o 的字符串形式。 成功時返回字符串,失敗時返回
NULL。 這相當于 Python 表達式repr(o)。 由內置函數repr()調用。在 3.4 版更改: 該函數現在包含一個調試斷言,用以確保不會靜默地丟棄活動的異常。
-
PyObject*
PyObject_ASCII(PyObject?*o)? - Return value: New reference.
與
PyObject_Repr()一樣,計算對象 o 的字符串形式,但在PyObject_Repr()返回的字符串中用\x、\u或\U轉義非 ASCII 字符。這將生成一個類似于 Python 2 中由PyObject_Repr()返回的字符串。由內置函數ascii()調用。
-
PyObject*
PyObject_Str(PyObject?*o)? - Return value: New reference.
計算對象 o 的字符串形式。 成功時返回字符串,失敗時返回
NULL。 這相當于 Python 表達式str(o)。由內置函數str()調用,因此也由print()函數調用。在 3.4 版更改: 該函數現在包含一個調試斷言,用以確保不會靜默地丟棄活動的異常。
-
PyObject*
PyObject_Bytes(PyObject?*o)? - Return value: New reference.
計算對象 o 的字節形式。失敗時返回
NULL,成功時返回一個字節串對象。這相當于 o 不是整數時的 Python 表達式bytes(o)。與bytes(o)不同的是,當 o 是整數而不是初始為 0 的字節串對象時,會觸發 TypeError。
-
int
PyObject_IsSubclass(PyObject?*derived, PyObject?*cls)? 如果 derived 類與 cls 類相同或為其派生類,則返回
1,否則返回0。 如果出錯則返回-1。如果 cls 是元組,則會對 cls 進行逐項檢測。如果至少有一次檢測返回
1,結果將為1,否則將是0。正如 PEP 3119 所述,如果 cls 帶有
__subclasscheck__()方法,將會被調用以確定子類的狀態。 否則,如果 derived 是個直接或間接子類,即包含在cls.__mro__中,那么它就是 cls 的一個子類。通常只有類對象才會被視為類,即
type或派生類的實例。然而,對象可以通過擁有__bases__屬性(必須是基類的元組)來覆蓋這一點。
-
int
PyObject_IsInstance(PyObject?*inst, PyObject?*cls)? 如果 inst 是 cls 類或其子類的實例,則返回
1,如果不是則返回``0``。 如果出錯則返回-1并設置一個異常。如果 cls 是元組,則會對 cls 進行逐項檢測。如果至少有一次檢測返回
1,結果將為1,否則將是0。正如 PEP 3119 所述,如果 cls 帶有
__subclasscheck__()方法,將會被調用以確定子類的狀態。 否則,如果 derived 是 cls 的子類,那么它就是 cls 的一個實例。實例 inst 可以通過
__class__屬性來覆蓋其所屬類。對象 cls 可以通過
__bases__屬性(必須是基類的元組)來覆蓋它是否被認作類的狀態,及其基類。
-
PyObject*
PyObject_Call(PyObject?*callable, PyObject?*args, PyObject?*kwargs)? - Return value: New reference.
調用一個可調用的 Python 對象 callable,附帶由元組 args 所給出的參數,以及由字典 kwargs 所給出的關鍵字參數。
args must not be
NULL, use an empty tuple if no arguments are needed. If no named arguments are needed, kwargs can beNULL.Return the result of the call on success, or raise an exception and return
NULLon failure.這等價于 Python 表達式
callable(*args, **kwargs)。
-
PyObject*
PyObject_CallObject(PyObject?*callable, PyObject?*args)? - Return value: New reference.
Call a callable Python object callable, with arguments given by the tuple args. If no arguments are needed, then args can be
NULL.Return the result of the call on success, or raise an exception and return
NULLon failure.這等價于 Python 表達式
callable(*args)。
-
PyObject*
PyObject_CallFunction(PyObject?*callable, const char?*format, ...)? - Return value: New reference.
Call a callable Python object callable, with a variable number of C arguments. The C arguments are described using a
Py_BuildValue()style format string. The format can beNULL, indicating that no arguments are provided.Return the result of the call on success, or raise an exception and return
NULLon failure.這等價于 Python 表達式
callable(*args)。Note that if you only pass
PyObject *args,PyObject_CallFunctionObjArgs()is a faster alternative.在 3.4 版更改: 這個 format 類型已從
char *更改。
-
PyObject*
PyObject_CallMethod(PyObject?*obj, const char?*name, const char?*format, ...)? - Return value: New reference.
Call the method named name of object obj with a variable number of C arguments. The C arguments are described by a
Py_BuildValue()format string that should produce a tuple.The format can be
NULL, indicating that no arguments are provided.Return the result of the call on success, or raise an exception and return
NULLon failure.這和Python表達式``obj.name(arg1, arg2, ...)``是一樣的。
Note that if you only pass
PyObject *args,PyObject_CallMethodObjArgs()is a faster alternative.在 3.4 版更改: The types of name and format were changed from
char *.
-
PyObject*
PyObject_CallFunctionObjArgs(PyObject?*callable, ..., NULL)? - Return value: New reference.
Call a callable Python object callable, with a variable number of
PyObject*arguments. The arguments are provided as a variable number of parameters followed byNULL.Return the result of the call on success, or raise an exception and return
NULLon failure.這和Python表達式``callable(arg1, arg2, ...)``是一樣的。
-
PyObject*
PyObject_CallMethodObjArgs(PyObject?*obj, PyObject?*name, ..., NULL)? - Return value: New reference.
Calls a method of the Python object obj, where the name of the method is given as a Python string object in name. It is called with a variable number of
PyObject*arguments. The arguments are provided as a variable number of parameters followed byNULL.Return the result of the call on success, or raise an exception and return
NULLon failure.
-
Py_hash_t
PyObject_Hash(PyObject?*o)? 計算并返回對象的哈希值 o。 失敗時返回
-1。這相當于 Python 表達式hash(o)。在 3.2 版更改: 現在的返回類型是 Py_hash_t。 這是一個帶符號整數,與 Py_ssize_t 大小相同。
-
Py_hash_t
PyObject_HashNotImplemented(PyObject?*o)? 設置一個
TypeError表示type(o)是不可哈希的,并返回-1。該函數保存在tp_hash槽中時會受到特別對待,允許某個類型向解釋器顯式表明它不可散列。
-
int
PyObject_IsTrue(PyObject?*o)? 如果對象 o 被認為是 true,則返回
1,否則返回0。這相當于 Python 表達式not not o。 失敗則返回-1。
-
PyObject*
PyObject_Type(PyObject?*o)? - Return value: New reference.
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 the common expressiono->ob_type, which returns a pointer of typePyTypeObject*, except when the incremented reference count is needed.
-
int
PyObject_TypeCheck(PyObject?*o, PyTypeObject?*type)? 如果對象Return true if the object o 為 type 類型或 type 的子類型則返回真值。 兩個參數都必須非
NULL。
-
Py_ssize_t
PyObject_Size(PyObject?*o)? -
Py_ssize_t
PyObject_Length(PyObject?*o)? 返回對象 o 的長度。 如果對象 o 支持序列和映射協議,則返回序列長度。 出錯時返回
-1。這等同于 Python 表達式len(o)。
-
Py_ssize_t
PyObject_LengthHint(PyObject?*o, Py_ssize_t?default)? Return an estimated length for the object o. First try to return its actual length, then an estimate using
__length_hint__(), and finally return the default value. On error return-1. This is the equivalent to the Python expressionoperator.length_hint(o, default).3.4 新版功能.
-
PyObject*
PyObject_GetItem(PyObject?*o, PyObject?*key)? - Return value: New reference.
返回對象 key 對應的 o 元素,或在失敗時返回
NULL。這等同于 Python 表達式o[key]。
-
int
PyObject_SetItem(PyObject?*o, PyObject?*key, PyObject?*v)? Map the object key to the value v. Raise an exception and return
-1on failure; return0on success. This is the equivalent of the Python statemento[key] = v.
-
int
PyObject_DelItem(PyObject?*o, PyObject?*key)? 從對象 o 中移除對象 key 的映射。失敗時返回
-1。 這相當于 Python 語句del o[key]。
-
PyObject*
PyObject_Dir(PyObject?*o)? - Return value: New reference.
相當于 Python 表達式
dir(o),返回一個(可能為空)適合對象參數的字符串列表,如果出錯則返回NULL。 如果參數為NULL,類似 Python 的dir(),則返回當前 locals 的名字;這時如果沒有活動的執行框架,則返回NULL,但PyErr_Occurred()將返回 false。
