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

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


I wrote:

# I want to do dd[item] += 1

Guido van Rossum wrote:

You don't need a new feature for that use case; d[k] = d.get(k, 0) + 1 is perfectly fine there and hard to improve upon.

Alex Martelli wrote:

I see d[k]+=1 as a substantial improvement -- conceptually more direct, "I've now seen one more k than I had seen before".

Guido van Rossum wrote:

Yes, I now agree. This means that I'm withdrawing proposal A (new method) and championing only B (a subclass that implements getitem() calling onmissing() and onmissing() defined in that subclass as before, calling defaultfactory unless it's None).

Probably already obvious from my previous post, but FWIW, +1.

Two unaddressed issues:

creating a defaultdict type is to provide a default, requiring two statements (the constructor call and the default_factory assignment) to initialize such a dictionary seems a little inconvenient.

STeVe

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



More information about the Python-Dev mailing list