Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the python "elif" compiled differently from else: if?

I know in languages such as C, C++, Java and C#, (C# example)the else if statement is syntactic sugar, in that it's really just a one else statement followed by an if statement.

else if (conition(s)) { ...

is equal to

else {
    if (condition(s)) { ...
}

However, in python, there is a special elif statement. I've been wondering if this is just shorthand for developers or if there is some hidden optimization python can do because of this, such as be interpreted faster? But this wouldn't make sense to me, as other languages would be doing it too then (such as JavaScript). So, my question is, in python is the elif statement just shorthand for the developers to use or is there something hidden that it gains through doing so?

like image 516
hargasinski Avatar asked Dec 16 '15 06:12

hargasinski


People also ask

What is the difference between Else and Elif in Python?

The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.

Is else necessary after Elif Python?

else is not required from the syntax point of view and is not enforced by PEP8 . If you intended do nothing if platform is not ios or android then this is perfectly ok. Save this answer.

Is Elif syntactic sugar?

elif in Python is syntactic sugar for else if seen in many other languages.

Why does Python have Elif?

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE. Similar to the else, the elif statement is optional.


2 Answers

When you really want to know what is going on behind the scenes in the interpreter, you can use the dis module. In this case:

>>> def f1():
...   if a:
...     b = 1
...   elif aa:
...     b = 2
... 
>>> def f2():
...   if a:
...     b = 1
...   else:
...     if aa:
...       b = 2
... 
>>> dis.dis(f1)
  2           0 LOAD_GLOBAL              0 (a)
              3 POP_JUMP_IF_FALSE       15

  3           6 LOAD_CONST               1 (1)
              9 STORE_FAST               0 (b)
             12 JUMP_FORWARD            15 (to 30)

  4     >>   15 LOAD_GLOBAL              1 (aa)
             18 POP_JUMP_IF_FALSE       30

  5          21 LOAD_CONST               2 (2)
             24 STORE_FAST               0 (b)
             27 JUMP_FORWARD             0 (to 30)
        >>   30 LOAD_CONST               0 (None)
             33 RETURN_VALUE        
>>> dis.dis(f2)
  2           0 LOAD_GLOBAL              0 (a)
              3 POP_JUMP_IF_FALSE       15

  3           6 LOAD_CONST               1 (1)
              9 STORE_FAST               0 (b)
             12 JUMP_FORWARD            15 (to 30)

  5     >>   15 LOAD_GLOBAL              1 (aa)
             18 POP_JUMP_IF_FALSE       30

  6          21 LOAD_CONST               2 (2)
             24 STORE_FAST               0 (b)
             27 JUMP_FORWARD             0 (to 30)
        >>   30 LOAD_CONST               0 (None)
             33 RETURN_VALUE        

It looks like our two functions are using the same bytecode -- So apparently they're equivalent.

Careful though, bytecode is an implementation detail of CPython -- There's no telling that all python implementations do the same thing behind the scenes -- All that matters is that they have the same behavior. Working through the logic, you can convince yourself that f1 and f2 should do the same thing regardless of whether the underlying implementation treats it as "syntatic sugar" or if there is something more sophisticated going on.

like image 132
mgilson Avatar answered Oct 02 '22 05:10

mgilson


The keyword ‘elif‘ is short for ‘else if’, and is useful to avoid excessive indentation.Source

like image 44
Prashant Yadav Avatar answered Oct 02 '22 04:10

Prashant Yadav