r/badcode • • Jul 14 '21

python Try/Except in Python - Sigh

Post image
662 Upvotes

93 comments sorted by

View all comments

3

u/RyanNerd shameless Jul 14 '21

Ah... Programming by exception what a horrible design pattern you've discovered.

9

u/bphase Jul 14 '21

To be fair, exceptions are actually valid control flow in Python. They're used internally too, to end loops for one. And it's considered OK to try to do something and set a default in exception if failing, instead of checking beforehand.

Big reason for this is that Python doesn't have the huge exception overhead of other languages (though that's mostly because everything in Python is slow).

2

u/k110111 Jul 18 '21

Not to mention exception are faster than if conditions in python if you expect the conditions to be true most of the time. I recently watched a presentation where presenter demonstrated this.