[Python-Dev] defaultdict proposal round three

Steven Bethard steven.bethard at gmail.com
Mon Feb 20 20:24:09 CET 2006

Guido van Rossum wrote:
> Alternative A: add a new method to the dict type with the semantics of
> __getattr__ from the last proposal, using default_factory if not None
> (except on_missing is inlined).

I'm not certain I understood this right but (after
s/__getattr__/__getitem__) this seems to suggest that for keeping a
dict of counts the code wouldn't really improve much:

dd = {}
dd.default_factory = int
for item in items:
    # I want to do ``dd[item] += 1`` but with a regular method instead
    # of __getitem__, this is not possible
    dd[item] = dd.somenewmethod(item) + 1

I don't think that's much better than just calling ``dd.get(item,
0)``.  Did I misunderstand Alternative A?

> Alternative B: provide a dict subclass that implements the __getattr__
> semantics from the last proposal.

If I didn't misinterpret Alternative A, I'd definitely prefer
Alternative B.  A dict of counts is by far my most common use case...

Grammar am for people who can't think for myself.
        --- Bucky Katt, Get Fuzzy

More information about the Python-Dev mailing list