映射協(xié)議?

參見(jiàn) PyObject_GetItem()、PyObject_SetItem()PyObject_DelItem()。

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

Return 1 if the object provides the mapping protocol or supports slicing, and 0 otherwise. Note that it returns 1 for Python classes with a __getitem__() method, since in general it is impossible to determine what type of keys the class supports. This function always succeeds.

Py_ssize_t PyMapping_Size(PyObject *o)?
Py_ssize_t PyMapping_Length(PyObject *o)?
Part of the Stable ABI.

成功時(shí)返回對象 o 中鍵的數量,失敗時(shí)返回 -1。 這相當于 Python 表達式 len(o)。

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

返回 o 中對應于字符串 key 的元素,或者失敗時(shí)返回 NULL。 這相當于 Python 表達式 o[key]。 另請參見(jiàn) also PyObject_GetItem()。

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

在對象 o 中將字符串 key 映射到值 v。 失敗時(shí)返回 -1。 這相當于 Python 語(yǔ)句 o[key] = v。 另請參見(jiàn) PyObject_SetItem()。 此函數 不會(huì ) 增加對 v 的引用。

int PyMapping_DelItem(PyObject *o, PyObject *key)?

從對象 o 中移除對象 key 的映射。 失敗時(shí)返回 -1。 這相當于 Python 語(yǔ)句 del o[key]。 這是 PyObject_DelItem() 的一個(gè)別名。

int PyMapping_DelItemString(PyObject *o, const char *key)?

從對象 o 中移除字符串 key 的映射。 失敗時(shí)返回 -1。 這相當于 Python 語(yǔ)句 del o[key]。

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

如果映射對象具有鍵 key 則返回 1,否則返回 0。 這相當于 Python 表達式 key in o。 此函數總是會(huì )成功執行。

請注意在調用 __getitem__() 方法期間發(fā)生的異常將會(huì )被屏蔽。 要獲取錯誤報告請改用 PyObject_GetItem()。

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

如果映射對象具有鍵 key 則返回 1,否則返回 0。 這相當于 Python 表達式 key in o。 此函數總是會(huì )成功執行。

請注意在調用 __getitem__() 方法期間發(fā)生的異常將會(huì )被屏蔽。 要獲取錯誤報告請改用 PyMapping_GetItemString()。

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

成功時(shí),返回對象 o 中的鍵的列表。 失敗時(shí),返回 NULL。

在 3.7 版更改: 在之前版本中,此函數返回一個(gè)列表或元組。

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

成功時(shí),返回對象 o 中的值的列表。 失敗時(shí),返回 NULL。

在 3.7 版更改: 在之前版本中,此函數返回一個(gè)列表或元組。

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

成功時(shí),返回對象 o 中條目的列表,其中每個(gè)條目是一個(gè)包含鍵值對的元組。 失敗時(shí),返回 NULL。

在 3.7 版更改: 在之前版本中,此函數返回一個(gè)列表或元組。