Search code examples
pythondictionarykeyerror

python get key for dict evaluates defulat part even when the key exists


My understanding of dict.get() was that if the key exists, then no evaluation of the default argument is made. And several sources implicitly leave this kind of interpretation. But it turns out that it is not the case.

Let's assume there is a dict that has integer keys either in str or int type. So, one line to retrieve a value module key type would be to use .get with a default value being dependent on the key of an alternative type.

>>> d = {'2':4} 
>>> d.get(2, d['2'])
4
>>> d.get('2', d[2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 2

What's the point of evaluating the default argument if the key exists? Is it just a bad design of the method or I am overgeneralize the intended functionality of .get()?


Solution

  • The main difference in this case is that the expression d[2] must be evaluated before it is passed as an argument to the get function.

    An exception occurs because you are referring in this expression to an integer key 2 that does not exist, before check if d['2'] key exists in that dictionary.

    Please find the reference: Expressions 6.3.4. Calls

    The primary must evaluate to a callable object (user-defined functions, built-in functions, ... and all objects having a __call__() method are callable). All argument expressions are evaluated before the call is attempted.