On 7/31/2010 11:08 AM, Christian Heimes wrote:

... All you have to do is subclass dict and implement a
__missing__ method. See
http://docs.python.org/library/stdtypes.html?highlight=__missing__#mapping-types-dict


Caveat -- there's another description of defaultdict here:

 http://docs.python.org/library/collections.html#collections.defaultdict

... and it's bogus. This other description claims that __missing__ is a method of defaultdict, not of dict.

This might cause considerable confusion, leading the reader to suspect that __missing__ and default_factory fight it out for the right to supply a default value. (__missing__ would win -- I tried it.)

The truth, as Christian says above and as Raymond Hettinger recently pointed out [1], is that __missing__ is used to *define* defaultdict as a subclass of dict -- it's not used *by* defaultdict.

-John

[1] http://mail.python.org/pipermail/python-list/2010-July/1248896.html
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to