Python pass statement

In Python, there is syntactical requirement that if statement, function, class, except etc., cannot be empty.

So, if there is a plan or chance that you will use the if statement, create a function, code an error handler etc. but not written statements to be executed, for now, then you may use a placeholder called pass there.

The Python pass statement is a null operation; nothing happens as pass statement is executed. However, the interpreter reads it and so if placed in functions, if statement, loops etc. this is taken as a statement.

A pass example pass with try-except

The interpreter will produce IndentationError if functions, except, loops etc. are left empty.

The pass statement can really be helpful for future implementation. As a programmer, you may want to move next part after the if block or a loop, creating an empty function or class, however, not sure what code to be written at that stage.

The use of pass statement can also go beyond place-holding. See an example of this scenario in the coming section.

Syntax of pass statement

The simple syntax of the pass statement is:

pass

A demo of using pass in if statement

See this example where the pass statement is used in the if statement. If you run this code, it will do nothing:

See online demo and code

 

If we did not use the pass statement an error would have produced.

An example of pass with for loop

This time, the pass statement is used inside a for loop:

See online demo and code

The pass statement with try except example

In this example, the pass statement is used in the try-except block. A list of five numbers is created and a for loop is used to iterate through its elements.

In each iteration, 15 is divided by the current item of the list. The list contains number 0 at three index position. As this item executes in the for loop for the division, it should throw the following error:

ZeroDivisionError: division by zero

However, as the try-except block is used, so the error is managed. We do not want to show any message or perform any action, just ignore the zero. Have a look at the code and I will explain the second part of the code:

See online demo and code

 

Python pass

A few things to note about this example: First, we used pass not as a place-holder in this example like in first two examples. It has a purpose, to ignore the 0 occurrences and we just don’t want to perform any action or display a message to the user.

The second thing is in the other for loop to iterate the list. You can see, no try-except block is used and the same list is divided by 15. As it found 0, it generated an error that is displayed on the screen.

Using pass in class and function example

Similarly, you may create empty function or class and use the pass statement as a placeholder. For example:

Creating a class:

class cls_pass:

pass

Creating a function:

def func_pass(x):

pass

If you do not use the pass statement, an error occurs SyntaxError (Unexpected EOF while parsing).