根据dict类型,什么是映射对象?

发布于 2021-01-29 17:25:36

文档列出了三种创建字典实例的方法:

class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)

这里的 映射 到底是什么?dict(mapping)工作所需的最小接口是什么?

关注者
0
被浏览
37
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    和往常一样,请仔细阅读代码:)

    因此,让我们进入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遵循该协议,那就太好了。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看