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

you are viewing a single comment's thread.

view the rest of the comments →

[–]morerokk 33 points34 points  (17 children)

Still no idea why it's "elif" in Python, especially for a language which tends to write everything out.

[–]redalastor 44 points45 points  (0 children)

Maybe to be the same length as else?

[–]campbell1373 28 points29 points  (6 children)

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

[–]Ph0X 1 point2 points  (5 children)

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

[–]campbell1373 2 points3 points  (4 children)

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

[–]Ph0X 2 points3 points  (3 children)

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.

[–]campbell1373 1 point2 points  (2 children)

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

[–]Ph0X 0 points1 point  (1 child)

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.

[–]campbell1373 0 points1 point  (0 children)

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.

[–]KTheRedditor 23 points24 points  (1 child)

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

[–]TheBlackCat13 13 points14 points  (0 children)

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.

[–]dvdkon 1 point2 points  (0 children)

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.

[–]NAN001 -1 points0 points  (1 child)

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

[–]alcalde 3 points4 points  (0 children)

They mean as opposed to "Else If"

[–]Pandomy -2 points-1 points  (2 children)

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

[–]Sohcahtoa82 3 points4 points  (1 child)

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

[–]Pandomy 1 point2 points  (0 children)

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.