default behavior
Christian Heimes
lists at cheimes.de
Tue Aug 3 18:35:30 EDT 2010
> I just went and read the entry that had the bogus claim -- personally, I
> didn't see any confusion. I would like to point out the __missing__ is
> *not* part of dicts (tested on 2.5 and 2.6 -- don't have 2.7 installed yet).
I beg your pardon but you are wrong. __missing__ is available for all
*subclasses* of dict since Python 2.5. See
http://svn.python.org/view/python/branches/release25-maint/Objects/dictobject.c?revision=81031&view=markup
>>> class mydict(dict):
... def __missing__(self, key):
... print "__missing__", key
... raise KeyError(key)
...
>>> m = mydict()
>>> m[1]
__missing__ 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __missing__
KeyError: 1
Christian
More information about the Python-list
mailing list