Is python "elif" compiled differently: if?

I know in languages ​​such as C, C ++, Java, and C #, ( C # Example ), the else if is syntactic sugar, because it really is just one else , followed by the if .

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

equally

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

However, in python there is a special elif operator. I was wondering if this is just a shorthand for developers, or if there is some kind of hidden optimization that python could do because of this, for example, being interpreted faster? But that would not make sense to me, as other languages ​​would do it too (e.g. JavaScript). So my question is, in python, does the elif operator simply reduce the use of developers or is there something hidden that it gets thanks to this?

+5
source share
4 answers

When you really want to know what happens 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 seems that our two functions use the same bytecode - so they seem to be equivalent.

Caution, however, bytecode is a detail of the implementation of CPython. It does not say that all python implementations do the same behind the scenes. All that matters is that they have the same behavior. By working in logic, you can convince yourself that f1 and f2 should do the same thing, regardless of whether the underlying implementation refers to it as “syntactic sugar” or if something more complex happens.

+11
source

The keyword 'elif' is short for 'else if, and it is useful to avoid over-indentation. A source

+3
source

elif in Python is syntactic sugar for else if , which can be seen in many other languages. It's all.

+1
source

The following three code snippets will be executed using the same logic, but everyone uses a different syntax.

 elif condition: ... 
 else if conition { ... 
 else { if conition { ... } 

Python elif is just syntactic sugar for the general else if

+1
source

Source: https://habr.com/ru/post/1238339/


All Articles