Pre-PEP: Dictionary accumulator methods
Michael Spencer
mahs at telcopartners.com
Sat Mar 19 18:46:42 EST 2005
Raymond Hettinger wrote:
> I would like to get everyone's thoughts on two new dictionary methods:
>
> def count(self, value, qty=1):
> try:
> self[key] += qty
> except KeyError:
> self[key] = qty
>
> def appendlist(self, key, *values):
> try:
> self[key].extend(values)
> except KeyError:
> self[key] = list(values)
>
These undoubtedly address common cases, which are unsatisfactory when spelled
using setdefault. However...
Use of these methods implicitly specializes the dictionary. The methods are
more-or-less mutually exclusive i.e., it would be at least strange to use count
and appendlist on the same dictionary. Moreover, on many dictionary instances,
the methods would fail or produce meaningless results.
This seems to be at odds with the other methods of built-in container types
which can be meaningfully applied, no matter what the types of the contents.
(There may be exceptions, but I can't think of any at the moment)
Does anyone else think this is a problem?
Michael
More information about the Python-list
mailing list