[issue10930] dict.setdefault: Bug: default argument is ALWAYS evaluated, i.e. no short-circuit eval

Amaury Forgeot d'Arc report at bugs.python.org
Tue Jan 18 13:14:19 CET 2011


Amaury Forgeot d'Arc <amauryfa at gmail.com> added the comment:

setdefault() is a method, its arguments are evaluated then the function is called. This is not a bug, and this behavior cannot change.

If you are trying to "cache" the computation of a function, you should try "memoizing" techniques, like the one mentioned here: http://code.activestate.com/recipes/52201-memoizing-cacheing-function-return-values/
Then you can write::

    @Memoize
    def fib(n):
        return fib(n-1) + fib(n-2)
    fib.memo = {(0,): 1, (1,): 1}

    @Memoize
    def func(n):
        return 1/float(n)
    func.memo = {(0.0,): infinite}

----------
nosy: +amaury.forgeotdarc
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10930>
_______________________________________


More information about the Python-bugs-list mailing list