This illustrates the difference between EAFP (Easier to ask forgiveness than permission) vs LBYL (Look before you leap).
To illustrate, consider a function which gets a value from a dictionary by key, returning a default if not present. This is the function implemented in each style:
def get(dictionary, key, default):
if key in dictionary:
return dictionary[key]
return default
def get(dictionary, key, default):
try:
return dictionary[key]
except KeyError:
return default
The difference is fairly arbitrary, and yet there is a general consensus for EAFP. I think this is a manifestation of the Zen of Python:
There should be one-- and preferably only one --obvious way to do it
The underlying idea is that it doesn't matter which is better, so long as we pick one and stick to it (i.e. bike-shedding is worse than picking a marginally inferior option).