Preferred Python idiom for handling non-existing dictionary keys and why?

Tim Peters tim.one at comcast.net
Fri Oct 10 23:25:48 EDT 2003


[Skip Montanaro]
>>> d.setdefault() never made any sense to me (IOW, to use it I always
>>> had to look it up).  The semantics of what it does just never stick
>>> in my brain.

[Tim]
>> Mentally change the name to getorset() and I bet it will be easier.
>> d.getorset(x, default) gets the value of d[x] if there is one
>> already, or sets d[x] to default and returns that.  I think Guido
>> would have changed the name to something sane, except he was pissed
>> at me that day <wink>.

[Mike Rovner]
> How to (persuade GvR to) add 'getorset' as an alias of setdefault to
> the language? ;)
> I also'd love to have it.

TOOWTDI is a strong force against adding aliases.  The trick is to get Guido
to think this renaming is so important as to justify using his time machine
so that the name was always getorset().

Alas, I haven't seen any evidence yet that the time machine survived his
move to California -- unless 2.3.1 is now named 2.3.1c1, and 2.3.2 named
2.3.1 (I keep checking every day -- that was an obvious and compelling use
for time-machine renaming).






More information about the Python-list mailing list