开发者

cpython: when PyDict_GetItem is called and when dict_subscript?

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜