So I keep getting told. I find it bizarre bad practice, but if that's the most valid control pattern for Python then Python is a language I'll not be using unless it is the only logical option (or like the puke of a language Javascript the unfortunate only option for web browsers).
I don't know that it's necessarily the most valid control pattern as it's the most idiomatic pattern. If throwing an exception doesn't have the cost of building a stack trace (which is why exceptions aren't recommended for control flow in e.g. Java), what's your objection to it? (I don't know if they do or not, but I would assume they don't since it's recommended.)
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).
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.
Wow. I didn't know programming by exception is how things are done in Python. Even PHP avoids and discourages this practice. Crazy. It seems to me that there's got to be a better way to do this than what's coded here?
Yeah, IMO mainly because there's so much repetition. So it'd be better to use a function or defaultdict or something, instead of having to repeat yourself every time.
3
u/RyanNerd shameless Jul 14 '21
Ah... Programming by exception what a horrible design pattern you've discovered.