[Python-Dev] Proposal: defaultdict
Steve Holden
steve at holdenweb.com
Fri Feb 17 07:09:26 CET 2006
Thomas Wouters wrote:
> On Thu, Feb 16, 2006 at 01:11:49PM -0800, 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.
>
>
> Should a dict subclass really change the constructor/initializer signature
> in an incompatible way?
>
Dict is a particularly difficult type to subclass anyway, given that it
can take an arbitrary number of arbitrarily-named keyword arguments
(among many other argument styles).
The proposed behavior is exactly how Icon tables behaved, and it was
indeed useful in that language. Guido is right about setdefault being a
busted flush.
If there's no way to resolve the signature issue (which there may not
be, given that
dict({'one': 2, 'two': 3})
dict({'one': 2, 'two': 3}.items())
dict({'one': 2, 'two': 3}.iteritems())
dict(zip(('one', 'two'), (2, 3)))
dict([['two', 3], ['one', 2]])
dict(one=2, two=3)
dict([(['one', 'two'][i-2], i) for i in (2, 3)])
are all valid calls to the type) then a factory function would be a very
acceptable substitute, no? (The function could make use of a subclass -
there's surely no necessity to provide the default as an initializer
argument: it could be provided as an argument to a method present only
in the subclass).
wishing-i-could-have-lunch-with-alex-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/
More information about the Python-Dev
mailing list