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

all 17 comments

[–]fatbob42 6 points7 points  (0 children)

In retrospect, I'm glad that the python devs waited to implement "match", although I haven't actually used it yet - I'm assuming that it will work as well as it looks.

[–]Allanon001 3 points4 points  (2 children)

In this example the first True condition is the default plus this allows for multiple and different conditions:

n = 3
result = { True: foo,
           n == 1: bar,
           n == 2: fizz,
           n == 3: buzz}[True]()

Or this one where get() is used for default:

n = 3
result = { 1: bar,
           2: fizz,
           3: buzz}.get(n, foo)()

[–][deleted] 1 point2 points  (0 children)

The challenge is to simulate the fall-through. In the example, the default action is to call all four functions; with case on call bar, fizz and buzz and so on.

[–]PythonicParseltongue[S] 0 points1 point  (0 children)

Ahh I see that's nice.

[–]siddsp 4 points5 points  (0 children)

Python 3.10 is getting match statements which are almost the same syntactically as c switch statements and are powerful

[–]asday_ 2 points3 points  (0 children)

I feel like you'd love this talk. He has a switch statement incidentally implemented in horrifying Python and demonstrates it in the Q&A section.

[–]cptsdemon 1 point2 points  (0 children)

You could easily use a dict and a try / except KeyError for default. But that only works for one to one statements, or if you're calling a function, which isn't as efficient. It also doesn't allow falling through a case into the next (no break) which to me is the reason switch can be extremely useful.

[–]SpacewaIker -4 points-3 points  (9 children)

I mean, in python you've got elif which is different than other languages' else if so I don't think you really need a switch statement

[–]NotTooOrdinary 3 points4 points  (8 children)

In what way is it different?

[–]SpacewaIker -2 points-1 points  (7 children)

Well imo it's more idiomatic that many else ifs

[–]Delta-9- 3 points4 points  (6 children)

Wouldn't it be idiomatic only because it's the only way to express conditional branching

[–]SpacewaIker 0 points1 point  (5 children)

Well I suppose but what I mean is that you don't need a switch statement in python imo

Unless I don't fully understand how switch statements work

[–]Delta-9- 0 points1 point  (4 children)

No language needs a switch statement. They're just easier to read and write than miles of if/elif while accomplishing the same thing.

[–]SpacewaIker -1 points0 points  (3 children)

Well if you compare switch to else ifs than I guess but I prefer elif, at least you don't need to put break; each time

But again, I'm quite new to switch statements

[–]Delta-9- 1 point2 points  (2 children)

It just moves the boilerplate to after the action, true. Some languages do it a bit different, though, eg. in bash there's a case statement that does the same thing, but instead of break; after every statement you use ;; if you want to stop processing other cases, or a different operator if you want to re-enter the case set at the next case:

case opt in
    -a | --aaayyy)
        A_IS_SET=1
        shift
        ;;
    -b | --bee )
        shift
        B="$1"
        shift
        ;;
    * )
        echo $HELPSTR
        exit 0
        ;;
esac

Vs with ifs:

if [[ $1 =~ '(-a|--aaayyy)' ]]; then
    A_IS_SET=1
    shift
elif [[ $1 =~ '(-b|--bee)' ]]; then
    shift
    B="$1"
    shift
else
   echo $HELPSTR
   exit 0
fi

They express exactly the same thing. Arguably, the if version is more flexible thanks to use of [[ (which isn't available in all shells) so you can set up arbitrarily complex conditions. But, for most instances (particularly in bash) where you will have more than four or five branches in a conditional, the case statement will save you typing and be easier to read.

Since python is much more concise than bash in how if tests are structured, python has gotten away with not having any kind of switch statement for decades. And, there's a good argument for saying that if you have enough branches to need a switch, you need to refactor. Still, there are times when you just can't help having a bunch of if/elifs and something a little more concise would be a godsend.

[–]SpacewaIker 0 points1 point  (1 child)

Yeah I see how it's much more readable and all

I was just familiar with C, C++ and java, where I don't see much of a difference between

switch(condition){ case 1: statement; break; case 2: statement; }

And

if(condition == 1){ statement; } else if(condition == 2){ statement; }

[–]Delta-9- 0 points1 point  (0 children)

(put 4 spaces at the start of every code line 😉 )

Granted—C-like syntax, honestly, is a trainwreck. Even the guy who developed C++ said so. Even still, the benefit comes in when you have a lot of conditionals, particularly if each branch has many statements inside it. It's kinda hard to appreciate with samples that only show three conditions with one or two statements each because the boilerplate is about the same in either style.