pass statement is used as a placeholder for an unwritten or unfinished section of code.
pass instructs the compiler to recognize a section of code as null, unlike comments which the compiler ignores entirely.
In programming, you may encounter a situation where you want to include a placeholder in a class, function, or procedure. For instance, you may be writing a large program and want to include a
for loop that does not yet function because there is other code you need to write first.
That’s where the Python
pass statement comes in. The pass statement is used as a placeholder for the future implementation of functions, classes, loops, and other blocks of code. The
pass statement is read by the compiler and recognized as a null operation.
This tutorial will discuss how to use the
pass statement in Python, and explore where the keyword may be useful. We’ll also walk through a few examples of the
pass statement being used in a Python program.
When you’re writing code, you may want to include a placeholder that serves as a reminder that a piece of code is still in progress and requires attention.
pass statement allows you to create placeholders in your code. The
pass statement is a null operation, so nothing happens when it runs. The Python interpreter still reads the code and processes it—unlike comments—but it reads it as a null statement.
Here’s the syntax for the pass statement:
pass statement takes in no arguments or variables; it is a standalone keyword.
For instance, let’s say you’re working on a program that stores bank account information for savings and checking accounts.
Before you start working on the savings account class, you want to focus on the checking accounts class. To remind yourself to come back to the savings account class later, and to create some structure in your program, you would need to add a placeholder in the savings account class.
Here’s the code you may use to add a placeholder to the savings account class while you build the checking account class:
class CheckingAccount(name): def __init__(self, name): self.name = name class SavingsAccount: pass
When our code runs, both the
SavingsAccount classes are created. However, the
SavingsAccount class has no purpose yet, and the
pass keyword is used to serve as a placeholder until we get around to finishing the class.
Python Pass Examples
There are a few scenarios where using the pass statement can be helpful. In the last example, we showed how a pass statement could be used with a class in Python.
While there are no specific guidelines around how pass statements should be used, the statement is required syntactically when:
- To define empty classes and functions because classes and functions cannot exist without code.
- To pass over code within a try except block.
- To pass over code in a loop.
Generally, pass statements are used as a placeholder while you develop the structure of a program, after which point they are replaced with new code.
Let’s look at an example of the pass statement being used to pass over code in a loop. Say we are a bookstore owner and we want to print out every title in our list of
habit formation books to the console. But, we don’t want to print out
Atomic Habits, which is out of stock. We could use the following code to accomplish this task:
books = ['The Power of Habit', 'Atomic Habits', 'Hooked: A Guide to Building Habit Forming Products', 'Mini Habits', 'The Miracle Morning'] for i in range(0, len(books)): if books[i] == 'Atomic Habits': pass else: print(books[i]) print('Program complete.')
Our code returns:
The Power of Habit Hooked: A Guide to Building Habit Forming Products Mini Habits The Miracle Morning Program complete.
There’s a lot going on in our code, so we should break it down. On the first line, we declare a list of books in the
habit formation category of our store.
We then declare a
for loop which loops through every book in the
books array. If the title of the book is
Atomic Habits, our code should skip over it; otherwise, our program should print out the title of the book to the console. Then, once our loop has finished running, our program prints out a message stating
Program complete. to the console.
In this code,
pass acts as a placeholder and allows us to skip over printing the name of the book
Atomic Habits to the console.
If we did not include a pass statement, our code would look like this:
... for i in range(0, len(books)): if books[i] == 'Atomic Habits': else: print(books[i]) …
When we run this code, Python returns an error because if statements cannot be empty. The error we receive is as follows:
IndentationError: expected an indented block
Perhaps, later on, we intend on adding additional code here, like printing a message saying that the book is out of stock, or perhaps we intend on leaving the code as it is. Either way,
pass acts as a placeholder in our code.
This tutorial discussed how to use the
pass statement in Python to include placeholder statements. We demonstrated how a pass statement may be used both in an empty class and in a for loop to act as a placeholder, and we talked about why a pass statement may be used.
Now you’re equipped with the information you need to use the Python
pass statement like a pro!