Pre-PEP: Dictionary accumulator methods

Roose b at b.b
Sun Mar 20 05:34:06 EST 2005


> How about the alternative approach of allowing the user to override the
> action to be taken when accessing a non-existent key?
>
>    d.defaultValue(0)

I like this a lot.  It makes it more clear from the code what is going on,
rather than having to figure out what the name appendlist, count, tally,
whatever, is supposed to mean.  When you see the value you'll know.

It's more general, because you can support dictionaries and sets then as
well.

I think someone mentioned that it might be a problem to add another piece of
state to all dicts though.  I don't know enough about the internals to say
anything about this.

setdefault gets around this by having you pass in the value every time, so
it doesn't have to store it.  It's very similar, but somehow many times more
awkward.






More information about the Python-list mailing list