The pass Statement in Python
In Python, the pass statement does nothing. It is used when a syntactically valid statement is required, but no action needs to be performed.
This article first explains the meaning of pass in Python and then describes how to use the pass statement with concrete examples.
What does the pass statement mean in Python
As you can see in the official documentation, the pass statement does nothing.
pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed
7. Simple statements - The pass statement — Python 3.11.3 documentation
In Python, the contents of the def statement in function definitions and the if statement in conditional branches cannot be omitted. You can use the pass statement when you need to write a syntactically valid statement but don't need to perform any action.
Difference between pass and continue
If the continue statement is used in the for or while loop, it will proceed to the next step without executing any code written after continue.
for i in range(3):
print(i)
if i == 1:
continue
print('CONTINUE')
# 0
# 1
# 2
On the other hand, the pass statement does nothing, so the code written after the pass statement is executed.
for i in range(3):
print(i)
if i == 1:
pass
print('PASS')
# 0
# 1
# PASS
# 2
Define an empty function and class with pass
You may need to define an empty function or class when you want to define a function or class first and then postpone the implementation.
If nothing is written in the def statement, an error is raised.
# def empty_func():
# SyntaxError: unexpected EOF while parsing
You can define an empty function that does nothing with the pass statement.
def empty_func():
pass
The same is true for class.
# class EmptyClass():
# SyntaxError: unexpected EOF while parsing
class EmptyClass():
pass
Although it is not recommended in PEP8 (E701), writing pass after a colon : does not raise an error.
- PEP 8 - Other Recommendations – Style Guide for Python Code | peps.python.org
- Error codes — pycodestyle 2.10.0 documentation
def empty_func_one_line(): pass
class EmptyClassOneLine(): pass
Create an empty file with pass
To create a new file, use with and open() in the write mode w.
Normally, you would use the write() method to write contents to a file, but you can create an empty file using the pass statement instead.
with open('temp/empty.txt', 'w'):
pass
You can also write it in a single line.
with open('temp/empty.txt', 'w'): pass
In Python 3.4 and later, it is also possible to create an empty file with touch() of the pathlib module.
Clarify that nothing is done in if ... elif ... else ... with pass
You cannot omit the contents of each block in if ... elif ... else ....
Use the pass statement when you want to postpone implementation or make the code's intent clearer by explicitly stating that no action is taken.
a = 3
if a % 2 == 0:
print('Even')
else:
pass
Do nothing in exception handling (= ignore exceptions) with pass
When an exception is raised, an error message is output, and the process is terminated.
def divide(a, b):
print(a / b)
# divide(1, 0)
# ZeroDivisionError: division by zero
You can use try to catch an exception and take action. If an exception is caught, the process continues without terminating.
def divide_exception(a, b):
try:
print(a / b)
except ZeroDivisionError as e:
print('ZeroDivisionError: ', e)
divide_exception(1, 0)
# ZeroDivisionError: division by zero
Use the pass statement if you want to catch an exception and do nothing. Even if an exception raises, the process can continue without doing anything.
def divide_exception_pass(a, b):
try:
print(a / b)
except ZeroDivisionError as e:
pass
divide_exception_pass(1, 0)
See the following article for details on exception handling in Python.