映射协议¶
参见 PyObject_GetItem()、PyObject_SetItem() 与 PyObject_DelItem()。
-
int PyMapping_Check(PyObject *o)¶
- Part of the Stable ABI.
如果对象提供了映射协议或是支持切片则返回
1,否则返回0。 请注意它将为具有__getitem__()方法的 Python 类返回1,因为在通常情况下无法确定该类支持哪种键类型。 此函数总是会成功执行。
-
Py_ssize_t PyMapping_Size(PyObject *o)¶
-
Py_ssize_t PyMapping_Length(PyObject *o)¶
- Part of the Stable ABI.
成功时返回对象 o 中键的数量,失败时返回
-1。 这相当于 Python 表达式len(o)。
-
PyObject *PyMapping_GetItemString(PyObject *o, const char *key)¶
- 返回值:新的引用。 Part of the Stable ABI.
这与
PyObject_GetItem()相同,但 key 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。
-
int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)¶
- Part of the Stable ABI since version 3.13.
Variant of
PyObject_GetItem()which doesn't raiseKeyErrorif the key is not found.If the key is found, return
1and set *result to a new strong reference to the corresponding value. If the key is not found, return0and set *result toNULL; theKeyErroris silenced. If an error other thanKeyErroris raised, return-1and set *result toNULL.在 3.13 版本加入.
-
int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)¶
- Part of the Stable ABI since version 3.13.
This is the same as
PyMapping_GetOptionalItem(), but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.在 3.13 版本加入.
-
int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)¶
- Part of the Stable ABI.
这与
PyObject_SetItem()相同,但 key 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。
-
int PyMapping_DelItem(PyObject *o, PyObject *key)¶
这是
PyObject_DelItem()的一个别名。
-
int PyMapping_DelItemString(PyObject *o, const char *key)¶
这与
PyObject_DelItem()相同,但 key 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。
-
int PyMapping_HasKeyWithError(PyObject *o, PyObject *key)¶
- Part of the Stable ABI since version 3.13.
Return
1if the mapping object has the key key and0otherwise. This is equivalent to the Python expressionkey in o. On failure, return-1.在 3.13 版本加入.
-
int PyMapping_HasKeyStringWithError(PyObject *o, const char *key)¶
- Part of the Stable ABI since version 3.13.
This is the same as
PyMapping_HasKeyWithError(), but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.在 3.13 版本加入.
-
int PyMapping_HasKey(PyObject *o, PyObject *key)¶
- Part of the Stable ABI.
如果映射对象具有键 key 则返回
1,否则返回0。 这相当于 Python 表达式key in o。 此函数总是会成功执行。备注
Exceptions which occur when this calls
__getitem__()method are silently ignored. For proper error handling, usePyMapping_HasKeyWithError(),PyMapping_GetOptionalItem()orPyObject_GetItem()instead.
-
int PyMapping_HasKeyString(PyObject *o, const char *key)¶
- Part of the Stable ABI.
这与
PyMapping_HasKey()相同,但 key 被指定为 const char* UTF-8 编码的字节串,而不是 PyObject*。备注
Exceptions that occur when this calls
__getitem__()method or while creating the temporarystrobject are silently ignored. For proper error handling, usePyMapping_HasKeyStringWithError(),PyMapping_GetOptionalItemString()orPyMapping_GetItemString()instead.
-
PyObject *PyMapping_Keys(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
成功时,返回对象 o 中的键的列表。 失败时,返回
NULL。在 3.7 版本发生变更: 在之前版本中,此函数返回一个列表或元组。
-
PyObject *PyMapping_Values(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
成功时,返回对象 o 中的值的列表。 失败时,返回
NULL。在 3.7 版本发生变更: 在之前版本中,此函数返回一个列表或元组。
-
PyObject *PyMapping_Items(PyObject *o)¶
- 返回值:新的引用。 Part of the Stable ABI.
成功时,返回对象 o 中条目的列表,其中每个条目是一个包含键值对的元组。 失败时,返回
NULL。在 3.7 版本发生变更: 在之前版本中,此函数返回一个列表或元组。