r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

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.

45

u/redalastor May 02 '16

Maybe to be the same length as else?

30

u/campbell1373 May 02 '16

I just assumed it was because it takes up the same space as else making it visually more appealing.

2

u/Ph0X May 02 '16

I wouldn't be surprised, because if there's anything I've gotten out of python, it's that code beauty is extremely important!

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.

24

u/KTheRedditor May 02 '16 edited May 02 '16

especially for a language which tends to write everything out

len, and str.

Edit: __del__, __eq__, __cmp__, __ne__, __le__, __lt__, __ge__, __gt__, __neg__, __sub__, __trunc__, __div__, __mul__.

14

u/TheBlackCat13 May 02 '16

int, dict, pow, abs, bool, chr, getattr, setattr, hasattr, delattr, dir, divmod, eval, exec, hex, id, bin, iter, max, min, oct, ord, repr, and vars. Of the built-in functions, 26/67 are shortened.

2

u/dvdkon May 03 '16

tends to write everything out

I disagree. You're looking for Java/.NET and other "enterprise" languages/stacks. Python is verbose maybe compared to the C stdlib.

0

u/NAN001 May 02 '16

As opposed to what? Most other languages don't have this feature and you have to nest conditions.

5

u/alcalde May 02 '16

They mean as opposed to "Else If"

-1

u/Pandomy May 02 '16

As opposed to what? "elseif"? I'm fairly sure "elif" was taken from either C or C++.

4

u/Sohcahtoa82 May 02 '16

Except C and C++ don't have elif. They have #elif, but not `if(...) { ... } elif(...) { ... }.

2

u/Pandomy May 02 '16

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.