This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict.get() evaluates second parameter even if key is present
Type: Stage:
Components: Build Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: claude_belisle, mcherm, rhettinger
Priority: normal Keywords:

Created on 2004-02-05 22:06 by claude_belisle, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg19923 - (view) Author: Claude Belisle (claude_belisle) Date: 2004-02-05 22:06
The optional parameter passed to get gets evaluated
even when the key is indeed present in the dictionary.
See attached file for the code I was using when
stumbling across this behaviour.
msg19924 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-02-05 23:07
Logged In: YES 
user_id=80475

Unfortunately, that it is what it is supposed to do.

Workaround:

try:
    v = mydict[k]
except AttributeError:
    v = defaultmaker()

msg19925 - (view) Author: Michael Chermside (mcherm) (Python triager) Date: 2004-02-13 12:35
Logged In: YES 
user_id=99874

That's the desired behavior. Python is never "lazy" (technical 
term)... it *always* evaluates all arguments to any function. 
In fact, they are evaluated *before* the function itself is 
invoked, so even if the function CAN decide that it doesn't 
need that parameter after all, it has already been evaluated.
History
Date User Action Args
2022-04-11 14:56:02adminsetgithub: 39905
2004-02-05 22:06:56claude_belislecreate