Let’s go for something a bit more esoteric. Suppose you wanted to execute a list of functions and store the result of each. For each function that raised an exception, you want to record the exception, and you also want to keep a count of how many times each kind of exception is raised. Functions and exceptions can be used as dict keys, so this is easy:
funclist = [foo, bar, baz, quux]
results = {}
badfuncs = {}
errorcount = {}
for f in funclist:
try:
results[f] = f()
except Exception as e:
badfuncs[f] = e
errorcount[type(e)] = errorcount[type(e)] + 1 if type(e) in errorcount else 1
Now you can do if foo in badfuncs to test whether that function raised an exception (or if foo in results to see if it ran properly), if ValueError in errorcount to see if any function raised ValueError, and so on.