根据dict类型,什么是映射对象?
该文档列出了三种创建字典实例的方法:
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
这里的 映射 到底是什么?dict(mapping)
工作所需的最小接口是什么?
-
和往常一样,请仔细阅读代码:)
因此,让我们进入
Include/dictobject.h
:132 /* PyDict_Merge updates/merges from a mapping object (an object that 133 supports PyMapping_Keys() and PyObject_GetItem()). If override is true, 134 the last occurrence of a key wins, else the first. The Python 135 dict.update(other) is equivalent to PyDict_Merge(dict, other, 1). 136 */
因此,我们正在寻找具有
PyMapping_Keys
和的事物PyObject_GetItem
。因为我们很懒,所以我们只使用python
docs中的搜索框并找到maps协议。因此,如果您的CPython
PyObject遵循该协议,那就太好了。