Python pass: The Complete Guide

Pass in Python is used when the statement is required syntactically, but you do not want that code to execute. The pass statement is the null operation; nothing happens when it runs.

Pass Python

Python pass is a built-in statement used as the placeholder for future implementation of functions, loops, etc. The pass is the null statement. The difference between a comment and the pass statement in Python is that, while the interpreter ignores the comment entirely, the pass statement is not ignored.

The pass statement is also helpful in scenarios where your code will eventually go but has not been entirely written yet.

Syntax

pass

Let’s see the example of the pass statement in Python.

# app.py

listA = [18, 21, 19, 29, 46]
for val in listA:
    pass

The above code will not do anything but does not throw any error. So that is the beauty of the pass statement.

Let’s see other examples with the class and functions in Python.

def function(args):
    pass

Here, if you call the function, you will not get any value or error, and you can do the same with a class.

class App:
    pass

The most used case of the pass statement is the scenario where you design a new class with some methods you do not want to implement yet.

Python Language has the syntactic requirement that code blocks, such as if, except, def, class, etc., cannot be empty. Empty code blocks are, however, useful in various contexts in the program.

Therefore, if nothing is supposed to happen in the code, the pass statement is needed for that block not to produce the IndentationError.

Alternatively, any statement (including just a term to be evaluated, like the Ellipsis literal … or a string, most often a docstring) can be used. Still, the pass statement makes clear that nothing is supposed to happen in that block of code and does not need to be run and (at least temporarily) stored in the memory.

That’s it for this tutorial.

1 thought on “Python pass: The Complete Guide”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.