Is get lazily evaluated?

python

What do you think about a dict.get operation does?

d.get(the_key, fall_back)

Something like this?

try:
    return d[the_key]
except KeyError:
    return fall_back

That is my intuition, just like C’s short-circuit evaluation, the fallback is not evaluated until the requested key does not exist. Unfortunately, this not try, for example:

>>> d = {1: 3, 2:4}
>>> def f():
... print 7
...
>>> d.get(3, f())
7
>>> d.get(1, f())
7
3

f is always evaluated regardless whether the key exists or not.

This is expected behavior from the compiler/interpreter perspective, the fallback value needs to be reduced first before get is invoked. It would be nice to evaluate get lazily to avoid the expensive try ... except mechanism; or tedious has_key test.