Choosing between different switch-case replacements in Python - dictionary or if-elif-else? Choosing between different switch-case replacements in Python - dictionary or if-elif-else? python python

Choosing between different switch-case replacements in Python - dictionary or if-elif-else?


Sigh. Too much hand-wringing over the wrong part of the problem. The switch statement is not the issue. There are many ways of expressing "alternative" that don't add meaning.

The issue is meaning -- not technical statement choices.

There are three common patterns.

  • Mapping a key to an object. Use a dictionary if it is almost totally static and you have a mapping between a simple key and another more complex thing. Building a dictionary on the fly each time you need it is silly. You can use this if it's what you mean: your "conditions" are simple, static key values that map to objects.

  • Variant behavior among subclasses. Use Polymorphism instead of type checking objects. Correct. If you have similar objects in multiple classes with variant behavior, they should be polymorphic. Use this as often as possible.

  • Other variant behavior. Use an if-elif-else ladder. Use this when you don't have largely static key-to-value mapping. Use this when the conditions are complex, or you mean procedures, not objects.

Everything else is just tricky code that can achieve similar results.

Using a Tuple. This is just dictionary without the mapping. This requires search, and search should be avoided whenever possible. Don't do this, it's inefficient. Use a dictionary.

Using a function decorator (http://code.activestate.com/recipes/440499/). Icky. This conceals the if-elif-elif nature of the problem you're solving. Don't do this, it isn't obvious that the choices are exclusive. Use anything else.

Someone even recommended the Visitor pattern. Use this when you have an object which follows the Composite design pattern. This depends on polymorphism to work, so it's not really a different solution.


In the first example I would certainly stick with the if-else statement. In fact I don't see a reason not to use if-else unless

  1. You find (using e.g. the profile module) that the if statement is a bottleneck (very unlikely IMO unless you have a huge number of cases that do very little)

  2. The code using a dictionary is clearer / has less repetition.

Your second example I would actually rewrite

setattr(self, name, value)

(probably adding an assert statement to catch invalid names).


Considering that this is done in response to a user action (pickings something from a menu), and the number of choices you anticipate is very small, I'd definitely go with a simple if-elif-else ladder.

There's no point in optinizing for speed, since it only happens as fast as the user can make the selection anyway, this is not "inner loop of a raytracer"-territory. Sure, it matters to give the user quick feedback, but since the number of cases is so small, there is no danger of that either.

There's no point in optimizing for conciseness, since the (imo clearer, zero-readability-overhead) if-ladder will be so very short anyway.