映射協(xié)議?

參見 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.

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

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

返回 o 中對應(yīng)于字符串 key 的元素,或者失敗時返回 NULL。 這相當(dāng)于 Python 表達式 o[key]。 另請參見 also PyObject_GetItem()。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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