Python Pass Statement

The pass statement does nothing when executed. You can use it as a placeholder for future code so that you can focus on the high-level structure first and implement the details later. For example, functions, loops, class definitions, or if statements require at least one statement in their indentation block. Think of the pass statement as a “todo” note that keeps your code syntactically intact.

Let’s explore it’s use for different types of indentation blocks.

Python Pass in Function Definition

You can use the pass statement as a body of your function definitions to keep the code syntactically intact and focus on the high-level structure first. At a later point in time you can then implement the function body when the rough structure is finalized.

In the following example, you create two dummy functions recognize_image() and read_image(). The functions do nothing yet but the code is already syntactically correct and you can implement the functionality later as you’ve finalized the high-level structure of your program.

def read_image():
    pass

def recognize_image(pixels):
    pass

pixels = read_image()
result = recognize_image(pixels)

print('Your Image Contains:')
print(result)

The output is as follows:

Your Image Contains:
None

Later you can fill-in-the-blanks by replacing the pass statements with your real code.

💡 If you were to leave out the pass, the code wouldn’t run because you would get an IndentationError: expected an indented block. To fix this, many coders use the pass statement as a “dummy” statement

IndentationError: expected an indented block

To summarize, the pass statement does nothing specifically but it often acts as a placeholder to be replaced with meaningful code later.

Python Pass in Method Definition

You can use the pass statement as a body of your method definitions to keep the code syntactically intact and focus on the high-level structure first. At a later point in time you can then implement the method body when the rough structure is finalized.

The following example creates a class Image with three methods:

  • The constructor method __init__() to create an instance of the class.
  • The method recognize() that performs some kind of image recognition algorithm to determine the content of the image.
  • The method read_from_file() that loads the data from an image file stored on your computer.

You don’t want to go into the nitty-gritty implementation just yet—so you create this high-level code first. As the method bodies consist only of the pass statement, the code doesn’t do anything but it already is syntactically correct and it helps you grasp the big picture quickly.

class Image:

    def __init__(self):
        pass


    def recognize(self):
        pass


    def read_from_file(self, filename):
        pass


img = Image()
print(img.recognize())
# None

Later you can fill-in-the-blanks by replacing the pass statements with your real code.

Python Pass in Class Definition

You can use the pass statement as a body of your class definition as well. In addition to the “todo” character discussed in the previous two sections, this has an additional benefit: You can create an empty object of a certain class and dynamically add methods to it!

In the following code, you create two instances data_1 and data_2 from the empty My_Data wrapper class that uses the pass statement to fill in the body with a dummy value.

After creation, you dynamically add new attributes to your instances such as a, b, c, enemy, and friend. This ability of Python to evaluate the type of an object dynamically and even change it after its creation is called duck-typing.

class My_Data:
    pass


data_1 = My_Data()
data_1.a = 'hi'
data_1.b = 42
data_1.c = 'friend'


data_2 = My_Data()
data_2.enemy = 'friend'
data_2.friend = 'enemy'


print(data_1.a, data_2.enemy)
# hi friend

Here, you’ve seen an alternative use of the pass statement where it was used to create an empty, flexible class definition.

Python Pass in If Block Definition

You can use the pass statement as a body of your if block. Again, the purpose is mainly to fill in the blanks later.

Here’s a simple example that takes some user input and checks whether the user typed in the value '42'.

user_input = input()

if user_input == '42':
    print('Yay!')
else:
    pass

Here are the outputs of two runs of this program where I put in 42 first and 41 second:

The idea here is to put your mental note to add the else branch later into the code so you don’t forget it.

Python Pass in Exception Handling

You can use the pass statement to ignore exceptions that have been caught. Without this statement, the exception is likely to terminate your program. But with the empty except block using a pass statement, the caught exception doesn’t terminate your program.

Here’s an example:

numbers = [12,1,0,45,56]
for number in numbers:
    try:
        print('result is {}'.format(1/number))
    except Exception as e:
        pass

The output is simply:

result is 0.08333333333333333
result is 1.0
result is 0.022222222222222223
result is 0.017857142857142856

No exception, no warning visible—even though we know it occurred for number=0 because we tried to divide by zero!

Summary

Syntactically, Python requires that code blocks such as if, except, def, class cannot be empty. However, empty code blocks are useful in many contexts. In these context, when nothing should be done in the code blocks, you can use the pass statement to avoid an IndentationError. Theoretically, you could use any other statement as well but most would have side-effects and per convention you should use the pass statement for an “empty” code block.