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.
views:
38answers:
1
A:
Observations, guesses, etc:
Same happens in Python 2.x.
dict_subscript
implements the equivalent of the high_level dict.__getitem__
method and thus will be called whenever adict[somekey]
appears other than on the LHS of an assignment in Python code.
PyDict_GetItem
is 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 using PyDict_GetItemWithError
instead ;-)
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
John Machin
2010-03-18 22:16:18