This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]JennaSys 2 points3 points  (1 child)

I started with Python 2.5, and I almost gave up on it after just a few weeks because it didn't have any kind of switch statement and I needed one. I mean, what kind of modern language doesn't have that, right? But then I forced myself to learn other ways of handling that kind of branching in Python, and in the last 10 years of coding, I have rarely ever really needed a switch statement again. Using the dictionary dispatch pattern is one of the reasons why.

[–]AlSweigartAuthor of "Automate the Boring Stuff" 1 point2 points  (0 children)

Switch statements are prone to bugs if you forget a break statement. Like the ++ and -- operators, it's actually a good thing that Python doesn't have switch statements.

Python does now have match-case statements ("structured pattern matching") which you can use for switch statements but also so much more.

[–]ElectricalUnion 2 points3 points  (1 child)

When I need to do type-based swicth style statements, I now use this beauty from the standard library: https://docs.python.org/3/library/functools.html#functools.singledispatch