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.

Author remi.lapeyre
Recipients remi.lapeyre, vykouk
Date 2019-05-24.09:45:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1558691120.39.0.318095054621.issue37033@roundup.psfhosted.org>
In-reply-to
Content
Hi @vykouk, Python evaluates arguments before calling functions so

>>> dicti.get(key, dflt(key))

is equivalent to:

>>> arg = dflt(key)
>>> dicti.get(key, arg)

Notive how dflt() is called before .get()

This is how all functions work in Python, not just dict methods.

You can change your code like so to make it work:

>>> if key in dicti:
>>>     x = dicti[key]
>>> else:
>>>     x = dflt(key)

or use defaultdict instead of setdefault which will take a callable to generate the default value.

I don't think there is a bug and we can close this issue.
Have a nice day.
History
Date User Action Args
2019-05-24 09:45:20remi.lapeyresetrecipients: + remi.lapeyre, vykouk
2019-05-24 09:45:20remi.lapeyresetmessageid: <1558691120.39.0.318095054621.issue37033@roundup.psfhosted.org>
2019-05-24 09:45:20remi.lapeyrelinkissue37033 messages
2019-05-24 09:45:20remi.lapeyrecreate