I am reading cpython code for python 3k and I have noticed, that __missing__ is called only when dict_subscript is called, but not when PyDict_GetItem is used. What is the difference between those two methods and when each is called? If I pass an PyObject that is a subclass of dict and has __missing__ method, how can I force using it, since PyDict_GetItem doesn’t do that.
I am reading cpython code for python 3k and I have noticed, that __missing__
Share
Observations, guesses, etc:
Same happens in Python 2.x.
dict_subscriptimplements the equivalent of the high_leveldict.__getitem__method and thus will be called wheneveradict[somekey]appears other than on the LHS of an assignment in Python code.PyDict_GetItemis part of the C API. Perhaps it’s an oversight that it hasn’t been updated.Having read the dire comments at the start of
PyDict_GetItem, I’d be usingPyDict_GetItemWithErrorinstead 😉Perhaps you can do the C-level equivalent of
my_getitem = getattr(my_dict, '__getitem__')once then call that.Perhaps you could raise a bug ticket or ask on comp.lang.python