Alternatives to switch statement from other languages
suggest changeRemarks
There is NO switch statement in python as a language design choice. There has been a PEP (PEP-3103) covering the topic that has been rejected. Python 3.10 later added match/case for pattern matching; that is a different statement, and the recipes below do not use it.
You can find many lists of recipes on how to do your own switch statements in python, and here I’m trying to suggest the most sensible options. Here are a few places to check:
- http://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python
- http://code.activestate.com/recipes/269708-some-python-style-switches/
- http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/
- …
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents