An error in Mapping Protocol doc

doc

It says:

int PyMapping_Check(PyObject *o)

Return 1 if the object provides the mapping protocol or supports slicing, and 0 otherwise.

I think or is wrong.Because in cpython/Objects/abstract.c.We have:

int
PyMapping_Check(PyObject *o)
{
    return o && Py_TYPE(o)->tp_as_mapping &&
        Py_TYPE(o)->tp_as_mapping->mp_subscript;
}

I think “Return 1 if the object provides the mapping protocol or supports slicing, and 0 otherwise.” should be
“Return 1 if the object provides the mapping protocol and supports slicing, and 0 otherwise.”

So dicts aren’t mappings? (Since they don’t support slicing.)

1 Like

In the code, we use && that is “and” in C.
So I think the “or” in doc should be changed into “and”.
Or maybe the code is wrong.

Both the code and the docs are correct. There is a bit of an optical illusion with the “and/or” and you may just need to puzzle it out. What I think you’re missing is that the mp_subscript slot can arise either from slicing OR from mapping. Also, the presence tp_as_mapping doesn’t mean “is a mapping”; instead, it means “uses one of the mapping slots”.

Thanks a lot!

As of Python 3.12, dicts support slice keys:

>>> d = {}
>>> d[1:2] = 'one to two'
>>> d
{slice(1, 2, None): 'one to two'}

and-the-times-they-are-a-changin-ly yours,

Raymond

1 Like

Hmm. Yes, I guess technically that’s slicing, but I don’t think I’d ever call it that.