[Python-Dev] defaultdict proposal round three (original) (raw)

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 defaultfactory if not None (except onmissing 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...

STeVe

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



More information about the Python-Dev mailing list