[Python-Dev] Proposal: defaultdict

Delaney, Timothy (Tim) tdelaney at avaya.com
Thu Feb 16 22:27:04 CET 2006


Guido van Rossum wrote:

> Over lunch with Alex Martelli, he proposed that a subclass of dict
> with this behavior (but implemented in C) would be a good addition to
> the language. It looks like it wouldn't be hard to implement. It could
> be a builtin named defaultdict. The first, required, argument to the
> constructor should be the default value. Remaining arguments (even
> keyword args) are passed unchanged to the dict constructor.
> 
> Feedback?

On behalf of everyone who has answered this question on c.l.py, may I
say WOOHOO!

FWIW, my usual spelling is:

    try:
        v = d[key]
    except:
        v = d[key] = value

which breaks the principle of "write it once".

Tim Delaney


More information about the Python-Dev mailing list