Pre-PEP: Dictionary accumulator methods

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sun Mar 20 05:25:14 EST 2005


Mike Rovner wrote:
> Paul Rubin wrote:
> 
>> If the compiler can do some type inference, it can optimize out those
>> multiple calls pretty straightforwardly.
> 
> It can be tipped like that:
> 
> di = dict(int)
> di.setdefault(0)
> di[key] += 1

Interesting, but why do you need to give the int type to the constructor?

> dl = dict(list)
> dl.setdefault([])
> dl.append("word")
> dl.extend(mylist)

I don't quite understand that. Which dict item are you extending? Don't
you need something like

dl[key].append("word")

?

Anyway, using `setdefault' as the method name is quite confusing,
although yours is IMHO a much better behavior given the name ;)

So what about `setdefaultvalue'?

Reinhold



More information about the Python-list mailing list