Search code examples
pythonlistpython-3.xdictionarypython-c-api

Do PyObject_GetItem and PyObject_SetItem work on PyType_List and PyType_Dict types?


Documentation for PyObject_GetItem and PyObject_SetItem here states:

PyObject* PyObject_GetItem(PyObject *o, PyObject *key) Return value: New reference.

Return element of o corresponding to the object key or NULL on failure. 
This is the equivalent of the Python expression o[key].

int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)

Map the object key to the value v. Returns -1 on failure. 
This is the equivalent of the Python statement o[key] = v.

foo[key] syntax implies PyType_Dict

However, it doesn't state whether it also works on PyType_List, in which case key would be an index, i.e. a positive PyType_Long, or maybe a type converts into it, e.g. a PyType_Bytes containing "42".

Does this function work for both containers?

I would expect it to; such a design to be in keeping with Python's "it does everything you would expect it to do" philosophy.

Furthermore, the project I'm looking at has a comment forewarning:

    // PyObject_SetItem is too weird to be using from C++
    // so it is intentionally omitted.

Should I be worried about this? What could it possibly mean? And has it been fixed for Python3?


Solution

  • Both functions work for all containers that support indexed accesses, be they dict, list, tuple, string, bytes, and so on.

    I'm not sure why PyCXX has that comment; it may be due to the fact that Python's dynamic typing does not always mesh well with languages with static typing.