The pass
statement in Python represents a null operation; it simply tells the interpreter to do nothing. That is correct, there are situations when you just want the program to do nothing.
Python does not allow empty blocks of code. All code blocks, including loops, if/else
statements, classes, functions, etc, must have at least one statement or a comment in order to be valid. Pass statements are commonly used in such situations so that the interpreter does not throw a Syntax error exception.
Consider the following examples:
Without a pass
statement.
a = 10
b = 5
if a > b:
as you can see above, the interpreter raises an error because empty blocks are just not allowed. Consider the following example with a pass statement.
With a pass statement
a = 10
b = 5
if a > b:
pass
Thus the pass
statement is useful when you want a code block to be syntactically valid, but you don't want it to do anything. It can also be used as a placeholders when you have not yet decided how to implement a particular part of the program.
pass
The syntax of the pass statement is that simple, just the pass
keyword without any other parameters. It can appear anywhere in the program whether inside a block or as a standalone statement.
Consider the following example:
for i in range(10):
if i % 2 == 0:
pass
else:
print(i)
In the above example, we have a loop to print the odd numbers from 0 to 9, we used the pass
statement inside the if
block so that if the number is even, the loop simply does 'nothing' while if the number is odd, it gets printed to the console.
Pass statement with functions and class
The pass statement can be useful when working with functions and classes as it allows you to define the structure of the function or class without actually implementing their functionality. For example, if you create a class and want to define its methods, you can use pass
statements within the class definition to initially define where the methods should go without actually implementing them.
Consider the following example.
Define the structure of a stack data structure.
class Stack:
def __init__(self):
pass
def push(self, v):
pass
def pop(self):
pass
def is_empty(self):
pass
def size(self):
pass
In the above example we prototyped how our stack data structure will look, including the methods it will contain. We used the pass
statement inside each method so as to establish the structure of the class before doing the actual implementation. This is a useful practice in initial phases of defining a class in order to plan out the features it will contain and how the code will be organized.
pass statement in exception handling
Normally, when an exception is raised we want to perform a corrective action or inform the user or administrators of the issue. There are sometimes, however, where we want to just do nothing when a certain exception is raised. We can use the pass statement in the except block to inform the interpreter that it should do nothing and continue with the normal flow of the program.
a = 10
b = 0
try:
c = a / b
print(c)
except ZeroDivisionError:
pass
In the above example, we divided 10 with 0 and , therefore, the ZeroDivisionError
was raised. Inside the except
block we used the pass
statement so as to just do nothing.
Note: There is a difference between using pass
statement and a comment for the same purpose. This is because, while a comment is entirely ignored, the pass statement gets executed and sends a message to the interpreter that nothing needs to be done. It produces a null operation, which doesn't interfere with the program in any way but serves as a placeholder or a dummy command.