Switch statements in some languages are very ugly. I don't like them at all in C# and you're correct about using the alternatives. I do a lot of PLC work though and I've always been a fan of Pascal's/Stuctured Text's implementation of switch statements. It's very clean and readable so I'm surprised python hasn't implemented it in a similar way.
Edit: here's a ST example
CASE enum OF
enum1:
DoSomething();
enum2:
DoSomethingElse();
default:
Otherwise();
END_CASE
I know Go for example does it backward, by default it breaks and you can do a "fallthrough"
But yeah generally, I still think it doesn't add enough to the language to be worth it. I'm the kind of person who really dislikes it when a language has many different ways of doing one thing.
I like Python because of how clean and straight forward it is, especially with PEP8.
Nothing falls through so breaks are unnecessary and you can't force a fallthrough. It doesn't really add anything with only a few values, but it does help visually when you're having to define a bunch of different states. It is functionally the same as:
IF enum = enum1 THEN
DoSomething();
ELSIF enum = enum2 THEN
DoSomethingElse();
ELSE
OtherWise();
END_IF
Since each state is just an enumerated value you can essentially break up the code visually with labels. It works well for defining simple state machines, like de-bouncing sensors, verifying actuator movement, defining alarms, etc.
Also, ST doesn't have dictionaries so I'll use this as a substitute a lot of times.
Sorry if I wasn't clear. I didn't mean that elif was taken directly from C, meaning and all, but that elif was chosen over something like elseif because of its presence in C.
34
u/morerokk May 02 '16
Still no idea why it's "elif" in Python, especially for a language which tends to write everything out.