dict literals vs dict(**kwds)

Duncan Booth duncan.booth at invalid.invalid
Fri May 26 16:25:22 CEST 2006

George Sakkis wrote:

> Perhaps you fail to understand that the given feature is
> 1) redundant (see above).

Yes, but a certain degree of redundancy in the language is inevitable, and 
when it exists (as in this case) to make people's life easier it may be a 
good thing. Obviously the tradeoff between increasing complexity vs 
increasing ease-of-use has to be discussed before such new features are 
added, and in this case I believe it was.

> 2) restricting in a more serious sense: the future addition of optional
> keyword arguments that affect the dict's behaviour. Google for "default
> dict" or "dictionary accumulator".

There is nothing to stop dictionaries being created using factory functions 
(e.g. see dict.fromkeys). So one possible way to implement defaults would 
be to extend the dict class with a new classmethod 'withdefault' which 
takes a default value as an argument.

However, given that the default argument isn't actually needed during 
construction, it doesn't seem to me that it fits either as a constructor 
parameter nor a factory method. I don't see why it shouldn't just be set on 
an existing dictionary (or dictionary subclass) when you need it.

More information about the Python-list mailing list