Skip to content
DevMeme
2596 of 7590
Languages Post #2873 · source on Telegram

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

62
Anonymous ★ Top Pick 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
  1. Anonymous ★ Top Pick

    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

  2. Anonymous

    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

  3. Anonymous

    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

  4. Anonymous

    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

  5. Anonymous

    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.”

  6. Anonymous

    Python vets don't miss switch - our dict dispatches have been pattern-matching fallthrough-free since the '90s

  7. Anonymous

    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.”

  8. @meowmeowang 5y

    3.10

  9. @xgoader 5y

    yep, it will be added in python 3.10

    1. @mvolfik 5y

      Ehh? You mean pattern matching? Or are they adding switch too 😳

      1. @Four_Velocity 5y

        https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-matching

        1. @RiedleroD 5y

          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.

          1. @RiedleroD 5y

            https://github.com/RiedleroD/pymark for anyone interested

  10. @zakaryan2004 5y

    3.10

  11. @dellism1 5y

    switch case is just a bunch of if conditions

    1. @zakaryan2004 5y

      That's the whole point, yes

    2. @VladislavSmolyanoy 5y

      In theory yes

    3. @Netawawa 5y

      Why do they exist then? Just to make code look better? Or they take less memory or smth?

      1. @dsmagikswsa 5y

        Switch after compile, it is just if else I think...so, It is for readability...

        1. @NoCountryForOldBuffet 5y

          Are you sure? If it's implemented with a lookup/jump table it should come out marginally faster

      2. @zakaryan2004 5y

        They don't, and can't take less memory

    4. @dugeru42 5y

      Take a break

  12. @dellism1 5y

    change my mind

  13. @man13hma 5y

    Switch case is a bad practice in every language

    1. @zakaryan2004 5y

      NoU

      1. @man13hma 5y

        ?

  14. @dellism1 5y

    switch case is a good behavior in coding i think

  15. @man13hma 5y

    No trolling. Switch case - bunch of ifs. To much of ifs - bad practice -> switch case - bad practice

    1. @RiedleroD 5y

      talking about stuff you don't understand → bad practice

  16. @NoCountryForOldBuffet 5y

    EG if you have cases 1-9, then you can jump to the default case (or skip the block) if you're > 9

    1. @dsmagikswsa 5y

      How are you sure you can jump to default without checking other case?

  17. @NoCountryForOldBuffet 5y

    Because compilers can check what the range of cases that exist are, and skip if it's outside that range

    1. @dsmagikswsa 5y

      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?

  18. @NoCountryForOldBuffet 5y

    Right, sometimes you can't optimize like that, and in those cases it's just a marginally more readable if-then-else block

  19. @NoCountryForOldBuffet 5y

    And yeah, when the compiler does the checks, it's doing the if-else, but the compiler isn't what executes the program

  20. @NoCountryForOldBuffet 5y

    The compiler can, in some cases, make code that is marginally more cycle efficient using switch than a naive if-elif-else block

    1. @dsmagikswsa 5y

      Good to know. I have a look about it. Thankyou

    2. @chupasaurus 5y

      The problem is python code usually isn't being compiled

  21. @serghei_k 5y

    if/elseif/else looks like pasta factory, so IMO this should be considered as a bad practice, not switch/case

  22. @NoCountryForOldBuffet 5y

    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

    1. @RiedleroD 5y

      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.

      1. @RiedleroD 5y

        pypy and cpython handle this way differently thought & could possibly still benefit from this

        1. @RiedleroD 5y

          once I get python 3.10 on my machine, I can run a few benchmarks & stuff

          1. Deleted Account 5y

            why even try running performance benchmarks on a python program

            1. @RiedleroD 5y

              mostly because it's fun, really.

              1. Deleted Account 5y

                it's mostly sad as for me

                1. @RiedleroD 5y

                  Idk I just like numbers & statistics.

            2. Deleted Account 5y

              also, isn't spm more like rust's match expression and not like c/c++ switch statement

              1. @RiedleroD 5y

                idk I don't know about rust.

                1. Deleted Account 5y

                  or the proposed c++23 match expression

                  1. @RiedleroD 5y

                    idk about that either tbh.

                    1. Deleted Account 5y

                      it does both structure decomposition and control flow

                      1. Deleted Account 5y

                        which is plain better than a switch and if

            3. @dugeru42 5y

              To determine which will be faster in production

              1. Deleted Account 5y

                PYTHON CODE IN PROD

  23. @FluffyClaws 5y

    Where the fuck is the neck pickup????

  24. @dnomin 5y

    Golang don't have try/catch 🌚

  25. @slnt_opp 5y

    Why one would even need it, if it's three statements - if elif else, if more - use maps

  26. @Agent1378 5y

    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

    1. @p4vook 5y

      Speed lover boo cringe

      1. @Agent1378 5y

        Yes, python isn't known for speed so why bother, right 😂

  27. @feskow 5y

    lover

Use J and K for navigation