3 ms·
#2 is because it is more "pythonic" to ask for forgiveness than permission (EAFP). For example, rather than checking if a dictionary has an element before acc
by mertd 10y ago
#2 is because it is more "pythonic" to ask for forgiveness than permission (EAFP).
For example, rather than checking if a dictionary has an element before accessing it, you just try to access it and handle the KeyError if it doesn't. The code reads easier and the normal flow would be faster in this case.
When used in EAFP context only, I think it is fine to use exceptions as part of logic in Python. They are relatively less costly compared to C.