Tricky Dictionary Question from newbie

Tim Peters tim.peters at gmail.com
Tue Jul 12 11:52:41 EDT 2005


[Peter Hansen]
...
> I suppose I shouldn't blame setdefault() itself for being poorly named,

No, you should blame Guido for that <wink>.

> but it's confusing to me each time I see it in the above, because the
> name doesn't emphasize that the value is being returned, and yet that
> fact is arguably more important than the fact that a default is set!
>
> I can't think of a better name, though, although I might find "foo" less
> confusing in the above context. :-)

I wanted to call it getorset() -- so much so that even now I sometimes
still type that instead!  The "get" part reminds me that it's fetching
a value, same as dict.get(key, default) -- "or set"'ing it too if
there's not already a value to "get".  If you have a fancy enough
editor, you can teach it to replace setdefault by getorset whenever
you type the former ;-)



More information about the Python-list mailing list