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

Albert report at bugs.python.org
Tue Jan 18 11:54:38 CET 2011


New submission from Albert <albert.neu at gmail.com>:

Hello!

Is it intentional, that the default argument is ALWAYS evaluated, even if it is not needed???

Is it not a bug, that this method has no short-circuit eval (http://en.wikipedia.org/wiki/Short-circuit_evaluation)
??

Example1:
=========
infinite = 1e100

one_div_by = {0.0 : infinite}

def func(n):
    return one_div_by.setdefault(float(n), 1/float(n))

for i in [1, 2, 3, 4]:
    print i, func(i)
print one_div_by
# works!!

for i in [0, 1, 2, 3, 4]:     # added 0 -> FAIL!
    print i, func(i)
print one_div_by
# fail!!



Example2:
=========
fib_d = {0 : 0, 1 : 1}

def fibonacci(n):
    return fib_d.setdefault(n, fibonacci(n-1) + fibonacci(n-2))

for i in range(10):
    print i, fibonacci(i)
print fib_d

----------
messages: 126456
nosy: albert.neu
priority: normal
severity: normal
status: open
title: dict.setdefault: Bug: default argument is ALWAYS evaluated, i.e. no short-circuit eval
type: behavior
versions: Python 2.6

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


More information about the Python-bugs-list mailing list