by BiteCode_dev · 6y ago · 0 comments
It also fits nicely into the context manager pattern.
Besides, remember exceptions are cheap in python. So cheap that for loops are basically syntaxic sugar for a try/catch.
EAFP - Easier to ask for forgiveness than permission
LBYL - Look before you leap
They are opposite strategies, Python's preferred strategy is to do the first, just try the operation and throw an exception if it doesn't work. The second is to check a pre-condition before attempting an operation.I've always known the second as exhibiting TOCTOU, otherwise known as a time-of-check vs time-of-use race condition.
Which is exactly how it's in all the programing languages and operating system calls which don't use exceptions for that.
It's just the question if one has to write try-catch for something as common as file not existing.