Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python dict get method runs the second argument even when key is in dict

according to the doc, the get method of a dict can have two argument: key and a value to return if that key is not in the dict. However, when the second argument is a function, it’ll run regardless of whether the key is in the dict or not:

def foo():
    print('foo')

params={'a':1}
print(params.get('a', foo()))
# foo
# 1

As shown above, the key a is in the dict, but foo() still runs. What happens here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a common misconception, the arguments must be evaluated whether or not they are used by the function, for example:

>>> def f(x, y, z): pass
... 
>>> f(print(1), print(2), print(3))
1
2
3

So the print(1), print(2), print(3) statements are executed even if the function f does not use the arguments. You can use try except instead to evaluate foo() only when it is needed:

try:
    x = params['a']
except KeyError:
    x = foo()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading