映射协议¶
参见 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.
This is the same as
PyObject_GetItem()
, but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.
-
int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)¶
- Part of the Stable ABI.
This is the same as
PyObject_SetItem()
, but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.
-
int PyMapping_DelItem(PyObject *o, PyObject *key)¶
This is an alias of
PyObject_DelItem()
.
-
int PyMapping_DelItemString(PyObject *o, const char *key)¶
This is the same as
PyObject_DelItem()
, but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.
-
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, usePyObject_GetItem()
instead.
-
int PyMapping_HasKeyString(PyObject *o, const char *key)¶
- Part of the Stable ABI.
This is the same as
PyMapping_HasKey()
, but key is specified as a const char* UTF-8 encoded bytes string, rather than a PyObject*.备注
Exceptions that occur when this calls
__getitem__()
method or while creating the temporarystr
object are silently ignored. For proper error handling, usePyMapping_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 版更改: 在之前版本中,此函数返回一个列表或元组。