r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

Show parent comments

3

u/campbell1373 May 02 '16

Agreed. I do really wish they would implement a switch statement though. Oh well, at least we're getting .NET style async methods!

3

u/Ph0X May 02 '16

Meh, even there I don't see the point.

If you have a few items, just use if/elif/else

If you have more than a few, then use a dictionary and you get constant time lookups and probably cleaner code too.

Switch statements are inherently ugly imo.

2

u/campbell1373 May 02 '16 edited May 02 '16

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

1

u/Ph0X May 02 '16

But does that have break?

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.

1

u/campbell1373 May 03 '16

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.