Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it best practice to include an else: pass statement at the end of a Python if or if/elif statement?

If I write:

if a == b:
    # do something
elif a == c:
    # do something else

and I just want to pass otherwise, is writing out the following required at the end?:

else:
    pass

It seems to run fine without the else: statement in the interpreter, is there a reason I'm not aware of that I should always include else: pass in these cases?

like image 581
YPCrumble Avatar asked Apr 04 '14 14:04

YPCrumble


People also ask

Is else necessary after Elif Python?

The if statements can be written without else or elif statements, But else and elif can't be used without else.

Is else pass necessary in Python?

An else pass is dead code, you should remove it, as it adds unnecessary noise to the code and anyway the code will be clearer and easier to understand without it.

What you must place at the end of an if if-else Elif statement?

The colon ( : ) is important because it separates the condition from the statements to be executed after the evaluation of the condition.

How do you end an Elif in Python?

To end the block, decrease the indentation. Subsequent statements after the block will be executed out of the if condition.


3 Answers

No, it isn't, the else suite is entirely optional.

From the if statement documentation:

if_stmt ::=  "if" expression ":" suite
             ( "elif" expression ":" suite )*
             ["else" ":" suite]

where (...)* means zero or more, and [...] means optional. So a valid if compound statement has an if line and suite, 0 or more elif lines and corresponding suites, and at most one else line and suite, which is optional.

The Python compiler will ignore any else: pass block, there is really no point in including it:

>>> import dis
>>> dis.dis(compile('''\
... if True:
...     foo
... else:
...     pass
... ''', '<stdin>', 'exec'))
  1           0 LOAD_NAME                0 (True)
              3 POP_JUMP_IF_FALSE       13

  2           6 LOAD_NAME                1 (foo)
              9 POP_TOP             
             10 JUMP_FORWARD             0 (to 13)

  4     >>   13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        
>>> dis.dis(compile('''\
... if True:
...     foo
... ''', '<stdin>', 'exec'))
  1           0 LOAD_NAME                0 (True)
              3 POP_JUMP_IF_FALSE       13

  2           6 LOAD_NAME                1 (foo)
              9 POP_TOP             
             10 JUMP_FORWARD             0 (to 13)
        >>   13 LOAD_CONST               0 (None)
             16 RETURN_VALUE        

where the only difference is the line number attached to the LOAD_CONST bytecode because of the extra lines in the first source sample.

Stylistically, else: pass is just clutter, something to reduce readability.

like image 106
Martijn Pieters Avatar answered Oct 11 '22 14:10

Martijn Pieters


Don't ignore a 'better practice' even though there may be no 'best practice'.

An 'else:' clause certainly is not required, however... Logic and syntax rules aside, using an 'if-elif-elif-elif...else: pass' block provides the perfect opportunity for the author to document what conditions have NOT yet been met after possibly many if-elif checks. In the future should an unexpected 'corner case' raise its ugly head this bit of documentation may very well help to find the bug.

like image 20
Hewey Dewey Avatar answered Oct 11 '22 16:10

Hewey Dewey


There is no need to put else after an if, or an elif statement. The only place that you would need to use one is if you want to do something if the if statement was not true. For example:

if a == b:
    myString = "true"
else:
    myString = "false";
like image 1
Jojodmo Avatar answered Oct 11 '22 16:10

Jojodmo