[Python-3000] A request to keep dict.setdefault() in 3.0
Guido van Rossum
guido at python.org
Mon Jul 9 23:01:15 CEST 2007
On 7/9/07, tav <tav at espians.com> wrote:
> setdefault's ability to return current value is also a very useful
> functionality and has saved writing:
>
> if key not in dict:
> value = <compute-value>
> dict[key] = value
>
> with the simpler:
>
> value = dict.setdefault(key, <compute-value>)
>
> Is there a better way to do the above without .setdefault?
Those are not equivalent, as the form using setdefault() *always*
evaluates <compute-value> while the other form only evaluates it when
needed.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list