[Python-Dev] Proposal: defaultdict
"Martin v. Löwis"
martin at v.loewis.de
Fri Feb 17 23:37:59 CET 2006
Guido van Rossum wrote:
> I'm torn. While trying to implement this I came across some ugliness
> in PyDict_GetItem() -- it would make sense if this also called
> on_missing(), but it must return a value without incrementing its
> refcount, and isn't supposed to raise exceptions -- so what to do if
> on_missing() returns a value that's not inserted in the dict?
I think there should be a guideline to use
PyObject_GetItem/PyMapping_GetItemString "normally", i.e. in all cases
where you would write d[k] in Python code.
It should be considered a bug if PyDict_GetItem is used in a place
that "should" invoke defaulting; IOW, the function should be reserved
to really low-level cases (e.g. if it is known that the dict doesn't
have any defaulting, e.g. the string interned dictionary).
There should be a policy whether name-lookup invokes defaulting
(i.e. __dict__ access); I think it should. This would cause
__getattr__ to have no effect if the object's dictionary has
a default factory (unless that raises a KeyError).
More information about the Python-Dev