Pre-PEP: Dictionary accumulator methods

George Sakkis gsakkis at rutgers.edu
Sat Mar 19 19:45:47 EST 2005


Michael Spencer wrote:
> 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


Yep, at least three more people in this thread:
- http://tinyurl.com/4bsdf
- http://tinyurl.com/3seqx
- http://tinyurl.com/6db27

George




More information about the Python-list mailing list