Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dict.setdefault: Bug: default argument is ALWAYS evaluated, i.e. no short-circuit eval #55139

Closed
albertneu mannequin opened this issue Jan 18, 2011 · 3 comments
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@albertneu
Copy link
Mannequin

albertneu mannequin commented Jan 18, 2011

BPO 10930
Nosy @amauryfa, @ericvsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2011-01-18.12:14:12.338>
created_at = <Date 2011-01-18.10:54:36.126>
labels = ['type-bug', 'invalid']
title = 'dict.setdefault: Bug: default argument is ALWAYS evaluated, i.e. no short-circuit eval'
updated_at = <Date 2011-01-18.15:28:04.916>
user = 'https://bugs.python.org/albertneu'

bugs.python.org fields:

activity = <Date 2011-01-18.15:28:04.916>
actor = 'eric.smith'
assignee = 'none'
closed = True
closed_date = <Date 2011-01-18.12:14:12.338>
closer = 'amaury.forgeotdarc'
components = []
creation = <Date 2011-01-18.10:54:36.126>
creator = 'albert.neu'
dependencies = []
files = []
hgrepos = []
issue_num = 10930
keywords = []
message_count = 3.0
messages = ['126456', '126458', '126467']
nosy_count = 3.0
nosy_names = ['amaury.forgeotdarc', 'eric.smith', 'albert.neu']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10930'
versions = ['Python 2.6']

@albertneu
Copy link
Mannequin Author

albertneu mannequin commented Jan 18, 2011

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

@albertneu albertneu mannequin added the type-bug An unexpected behavior, bug, or error label Jan 18, 2011
@amauryfa
Copy link
Member

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}

@ericvsmith
Copy link
Member

Or use a collections.defaultdict, which has a factory function as a constructor argument. It sort of depends on what you're trying to do.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants