Python Pass

In this section, you will learn Python pass statement.

Python pass Statement

pass is actually a null statement which is generally used as a placeholder. When you want to declare a function or a loop but not want to provide the implementation then you can use pass statement. It is similar to ; in C programming language or nop in assembly language.

The pass statement will result in no operation (NOP) which means nothing happens when pass is executed.

So what is the difference between pass and Python comments when nothing happens to both of them?

The comments are ignored and not executed, but pass statement will be executed resulting in nothing.

The following is the syntax of pass in Python:

pass

If you want to provide the implementation of a loop or function in future then you have to use pass statement because a function or a loop can never have an empty body in Python.

pass statement creates an empty body for you.

pass Statement Example:

l = ['p', 'y', 't', 'h', 'o', 'n']
for i in l:
    pass

So here, for loop has an empty body indicated by pass statement. If there is no pass statement and body of for is left empty you will have a SyntaxError - expected an indented block.

Similarly, pass can also be used in classes and functions when you plan to implement classes and function in the future. Consider the example below:

def function(args):
    pass
class ABC:
    pass