`if key in dict` vs. `try/except` – which is more readable idiom?

Exceptions are not conditionals. The conditional version is clearer. That’s natural: this is straightforward flow control, which is what conditionals are designed for, not exceptions. The exception version is primarily used as an optimization when doing these lookups in a loop: for some algorithms it allows eliminating tests from inner loops. It doesn’t have that … Read more

Too many if statements

You could possibly use a dictionary. Dictionaries store references, which means functions are perfectly viable to use, like so: operationFuncs = { Operation.START: strategy_objects.StartObject Operation.STOP: strategy_objects.StopObject Operation.STATUS: strategy_objects.StatusObject (…) } It’s good to have a default operation just in case, so when you run it use a try except and handle the exception (ie. the … Read more