[Python-Dev] defaultdict proposal round three

Raymond Hettinger python at rcn.com
Tue Feb 21 00:14:13 CET 2006


[Steven Bethard]
> * Should default_factory be an argument to the constructor?  The three
> answers I see:
>
>  - "No."  I'm not a big fan of this answer.  Since the whole point of
> creating a defaultdict type is to provide a default, requiring two
> statements (the constructor call and the default_factory assignment)
> to initialize such a dictionary seems a little inconvenient.

You still have to allow assignments to the default_factory attribute to allow 
the factory to be changed:

    dd.default_factory = SomeFactory

If it's too much effort to do the initial setup in two lines, a classmethod 
could serve as an alternate constructor (leaving the regular contructor fully 
interchangeable with dicts):

    dd = defaultdict.setup(list, {'k1':'v1', 'k2:v2'})

or when there are no initial values:

    dd = defaultdict.setup(list)


Raymond


More information about the Python-Dev mailing list