Python's Missing Switch Case and Van Halen's Logical Leap
Description
A two-part meme. The top text on a white background reads, 'When you realise python doesn't have a switch case:'. Below this is a photograph of a smiling Eddie Van Halen on stage, wearing a yellow tank top and holding his iconic red, white, and black striped guitar. A caption overlaid on the bottom of the image reads, 'Might as well jump'. The meme humorously connects a specific programming language feature - or lack thereof - with a classic rock anthem. For developers, the absence of a traditional switch-case statement in Python is a well-known characteristic, often leading to workarounds with dictionaries or if-elif-else chains. The punchline 'Might as well jump' is a literal quote from the Van Halen song 'Jump', comically suggesting a dramatic or musically-inspired reaction to this language design choice. It also serves as a clever pun on control flow 'jumps' in programming
Comments
62Comment deleted
Python's philosophy is 'There should be one-- and preferably only one --obvious way to do it.' For switch cases, the obvious way is apparently a dictionary of functions, which feels less like a jump and more like a carefully mapped out teleport
I survived 15 years riffing through if/elif solos; then Python drops match/case and everyone acts like pattern matching is a brand-new genre - thanks for releasing the remastered switch album, Guido
After 30 years of if-elif chains and dictionary dispatch patterns, Python finally got match/case in 3.10 - just in time for everyone to have already written their own switch implementation using lambdas, decorators, and three different metaclasses
Ah yes, the classic Python moment: you're about to elegantly handle multiple conditions with a switch statement, only to remember Guido decided 'explicit is better than implicit' meant writing 47 elif clauses instead. Sure, Python 3.10 gave us match/case, but by then we'd already built entire careers on nested if-elif chains and dictionary dispatch patterns. At least we can console ourselves that our code is 'readable' - assuming your definition of readable includes scrolling through a vertical tower of conditionals that would make the Tower of Babel jealous
Python didn’t skip switch; it jumped to match-case - now half the codebase is dispatch dicts, the other half is a mini‑Prolog, and everyone calls it “pythonic.”
Python vets don't miss switch - our dict dispatches have been pattern-matching fallthrough-free since the '90s
Senior workaround: build a dict-of-callables and call it a jump table - then 3.10 ships match/case and the architecture board says, “great, but keep our 2014 DispatcherFactory for consistency.”
3.10 Comment deleted
yep, it will be added in python 3.10 Comment deleted
Ehh? You mean pattern matching? Or are they adding switch too 😳 Comment deleted
https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-matching Comment deleted
ah, which reminds me: I've done a little testing & structural pattern matching is already almost as fast as simple if/elif. I believe there will be a lot more optimisation in the following months, since it's very early in development. Source Code will be uploaded asap, if you guys want to take a look. Comment deleted
https://github.com/RiedleroD/pymark for anyone interested Comment deleted
3.10 Comment deleted
switch case is just a bunch of if conditions Comment deleted
That's the whole point, yes Comment deleted
In theory yes Comment deleted
Why do they exist then? Just to make code look better? Or they take less memory or smth? Comment deleted
Switch after compile, it is just if else I think...so, It is for readability... Comment deleted
Are you sure? If it's implemented with a lookup/jump table it should come out marginally faster Comment deleted
They don't, and can't take less memory Comment deleted
Take a break Comment deleted
change my mind Comment deleted
Switch case is a bad practice in every language Comment deleted
NoU Comment deleted
? Comment deleted
switch case is a good behavior in coding i think Comment deleted
No trolling. Switch case - bunch of ifs. To much of ifs - bad practice -> switch case - bad practice Comment deleted
talking about stuff you don't understand → bad practice Comment deleted
EG if you have cases 1-9, then you can jump to the default case (or skip the block) if you're > 9 Comment deleted
How are you sure you can jump to default without checking other case? Comment deleted
Because compilers can check what the range of cases that exist are, and skip if it's outside that range Comment deleted
Sometimes there is no range, right? It is not necessary a range of number. At the same time, when the compiler check, it sort of doing the if else? Comment deleted
Right, sometimes you can't optimize like that, and in those cases it's just a marginally more readable if-then-else block Comment deleted
And yeah, when the compiler does the checks, it's doing the if-else, but the compiler isn't what executes the program Comment deleted
The compiler can, in some cases, make code that is marginally more cycle efficient using switch than a naive if-elif-else block Comment deleted
Good to know. I have a look about it. Thankyou Comment deleted
The problem is python code usually isn't being compiled Comment deleted
if/elseif/else looks like pasta factory, so IMO this should be considered as a bad practice, not switch/case Comment deleted
Yeah, I was speaking theoretically, for python I don't think it matters, and I doubt the main compilers would bother implementing it with a jump table Comment deleted
well the main compilers are cython and nuitka, which would both probably just compile it to an actual switch case statement in C/C++ unless some fancy stuff is happening that C/C++ doesn't support in a switch/case statement. Comment deleted
pypy and cpython handle this way differently thought & could possibly still benefit from this Comment deleted
once I get python 3.10 on my machine, I can run a few benchmarks & stuff Comment deleted
why even try running performance benchmarks on a python program Comment deleted
mostly because it's fun, really. Comment deleted
it's mostly sad as for me Comment deleted
Idk I just like numbers & statistics. Comment deleted
also, isn't spm more like rust's match expression and not like c/c++ switch statement Comment deleted
idk I don't know about rust. Comment deleted
or the proposed c++23 match expression Comment deleted
idk about that either tbh. Comment deleted
it does both structure decomposition and control flow Comment deleted
which is plain better than a switch and if Comment deleted
To determine which will be faster in production Comment deleted
PYTHON CODE IN PROD Comment deleted
Where the fuck is the neck pickup???? Comment deleted
Golang don't have try/catch 🌚 Comment deleted
Why one would even need it, if it's three statements - if elif else, if more - use maps Comment deleted
In switch a compiler/interpreter can create a jump table, so instead of checking all the values like in multiple if you get to do one check in a constant time. Very effective Comment deleted
Speed lover boo cringe Comment deleted
Yes, python isn't known for speed so why bother, right 😂 Comment deleted
lover Comment deleted