In Python, there is a 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.
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:
#A demo of using pass statement str_p = 'Python' if str_p == 'Python': pass
If we did not use the pass statement an error would have been produced.
An example of pass with for loop
This time, the pass statement is used inside a for loop:
#pass statement with for loop a_List = ['pass', 'statement', 'example', 'in', 'for', 'loop'] for val in a_List: pass
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:
#The pass example with try except List_num = [5, 10, 20, 0, 40] b = 15 for x in List_num: try: c = b / x print(c) except ZeroDivisionError: pass print("###################") for y in List_num: z = b / y print(z)
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).