Python, why the elif keyword?

I just started programming in Python, and I wonder about the elif keyword.

Other programming languages ​​that I used before using else if . Does anyone have an idea why the Python developers added an extra elif keyword?

Why not:

 if a: print("a") else if b: print("b") else: print("c") 
+52
python keyword
Sep 18 '10 at 16:54
source share
8 answers

Most likely it is syntactic sugar. Like Wend Visual Basic.

+1
Sep 18 '10 at 17:00
source share

As far as I know, it is there to avoid excessive indentation. You can write

 if x < 0: print 'Negative' else: if x == 0: print 'Zero' else: print 'Positive' 

but

 if x < 0: print 'Negative' elif x == 0: print 'Zero' else: print 'Positive' 

much nicer.




Thanks to ign for docs link:

The elif keyword is short for "else if" and it is helpful to avoid over-indentation.

+114
Sep 18 '10 at 17:01
source share

Languages ​​with C-type syntax get else if for free without doing it at all.

The reason is that in these syntactic control structures, they simply work with the following statement, which can be a compound expression enclosed in curly braces, if necessary (for example, { x += 1; y += 1 } ).

This means that after you have implemented if and else , else if just disappears from the grammar of the language, naturally, for free, without any further implementation efforts. To understand why, look at this:

 if (condition) { if_body(); } else if (another_condition) { else_if_body(); } else { else_body(); } 

It looks like an if with attached else if and else , each of which is applied to the compound statement. But actually it is not. In fact, these are two separate if , each of which has exactly one else case; the second if is inside the else body of the first if .

else if { ... } really parsed as else applied to the next statement, which is the if (applied to the compound expression { else_if_body(); } . Then the final else bound to the previous previous if , which is the second.

Here the same is written more according to how it was analyzed 1 :

 if (condition) { if_body(); } else { if (another_condition) { else_if_body(); } else { else_body(); } } 

But it turns out that if the language directly implemented else if as a first-class option for if , it will behave exactly like the second independent if inside else first! Thus, there is no need to worry about implementing else if at all; language developers get an else if for free with this syntax style as soon as they implement if and else .

Python syntax does not allow this freebie.

C-style syntax programmers can think in terms of else if , although the language has an if only with zero or one else , but only because they can write code, like my first example, which is formatted in a way that is different from the human reader, than with the compiler.

Python, OTOH, uses padding to indicate the block structure, which makes the block structure look the same with the human reader, as with interpreter 2 . After you received if and else in the Python style syntax, programmers could still write code that behaves identically to else-if by placing the second if inside the else first. But it looks like this:

 if condition: if_body() else: if another_condition: else_if_body() else: else_body() 

It looks ugly and much more difficult to think in terms than the else-if chain when you get more than 1 or 2 else-ifs. Therefore, it is worth adding an explicit language function to restore the ability to think in terms of else-if. Even though it technically makes the language more complex, it actually makes thinking from the point of view of the language simpler, so this is a good complexity; with a manually created chain of nested if inside else reader must manually read the entire code and make sure that every else except the last contains exactly one if and nothing else to conclude that the whole sequence is equivalent to a linear chain of conditions checked in order, with some code to perform the first check, which succeeds.

So then. We have seen that languages ​​with C-type syntax can also be used with else if , because they get it for free. This is the reason why it exists. Languages ​​with syntax like Python must explicitly do something to get a construct that can be used as else-if. Why did they choose elif ? It is arbitrary; you will actually have to ask the people who made the decision.

However, Python did not invent elif ; it was in other languages ​​long before Python existed. Therefore, I would suggest that when they had to implement the explicit else-if construct, they simply chose the one that programmers are already familiar with.




1 Technically, that’s the way people who REALLY take seriously the use of braces with control structures should write their code .;)

2 You can, of course, create counter-examples for this, but this is a general idea of ​​syntax based on indentation.

+56
Oct 27 2018-12-12T00:
source share

To avoid brackets ^ H ^ H ^ H ^ H ^ Help if war.

In C / C ++, where you have else if , you can structure your code in many different styles:

 if (...) { ... } else if (...) { ... } if (...) { ... } else if (...) { ... } if (...) { ... } else if (...) { ... } // and so on 

having elif instead, such a war will never happen, since there is only one way to write elif . In addition, elif much shorter than else if .

+17
Sep 18 '10 at 17:06
source share

It is as it is. Javascript uses else if , php uses elseif , perl uses elsif , the C preprocessor and python use elif . None of them are mistaken, they just choose a slightly different syntax to do the same: D

+9
Sep 18 '10 at 17:08
source share

I find them useful to help distinguish "else-if" from "final else".

+6
May 20 '11 at 9:52 p.m.
source share

elif is a kind of switch replacement in other languages, but with more power

for example, in C you write

 switch (number) {
  case 1:
    doA ()
    break;
  case 2:
    doB ()
    break;
  case N:
    doN ()
    break;
  default:
    doSomethingElse ()
    break;
 }

in Python you write

 if number == 1: doA ()
 elif number == 2: doB ()
 elif number == N: doC ()
 else: doSomethingElse ()

As you can see, elif is more powerful, since you can enter more complex statements than on the switch, and also avoid nesting if/else

0
Sep 18 2018-10-18
source share

Python inherits this from Perl, where it is called elsif .

In the case of Python, else if , since two separate constructs like this one in C-like languages ​​will be pretty ugly, since you need to have else: if: with two levels of indentation.

Is it possible that a combination of two keywords would be better (so else if create one construct, for example, the not in operator.

PL / SQL also has elsif , and the C preprocessor has an elif value.

-6
Sep 18 '10 at 17:03
source share



All Articles