A try/except block is extremely efficient. Actually executing an exception is expensive. In versions of Python prior to 2.0 it was common to use this idiom:

try:
    value = dict[key]
except KeyError:
    dict[key] = getvalue(key)
    value = dict[key]

This only made sense when you expected the dict to have the key almost all the time. If that wasn't the case, you coded it like this:

if dict.has_key(key):
    value = dict[key]
else:
    dict[key] = getvalue(key)
    value = dict[key]

(In Python 2.0 and higher, you can code this as value = dict.setdefault(key, getvalue(key)).)

CATEGORY: general