Python provides us with some unique functionalities that don’t exist in other languages. One such functionality is the pass keyword. In this article, we will understand the working of the Python pass keyword with examples.
What is the pass Keyword in Python?
The pass keyword in Python is a special keyword used to define a statement that does nothing. When you need to write a Python statement that does nothing, you can write it using the pass keyword as shown below.
print("PFB") pass print("PythonForBeginners")
In the above code, we have written a pass statement between two print statements. You can observe that the print statements produce the output while the pass statement does nothing.
Functionally, you might say that the pass statement is similar to Python comments. However, their functioning is entirely different. Comments in Python are ignored by the Python interpreter. On the other hand, the pass statement is parsed and executed by the Python interpreter.
When to Use The pass Statement in Python?
We can use the pass keyword in Python in two situations.
- When we don’t want to do anything if a certain condition occurs.
- When we don’t know exactly what you need to do. There might be situations where you need to implement a function, class, or method but you haven’t decided on the implementation yet. In such cases, you can use the pass keyword as a placeholder for function implementation. This will help you define the implementation of the functions or methods and implement them later.
Let us discuss the use of the pass keyword in both of these situations.
The Pass Keyword as a Placeholder in Conditional Statements in Python
Suppose that we have multiple conditional statements and we don’t want to perform any action when a particular condition occurs. In such a case, if we include a comment for the particular condition and do not write any valid Python statements, the program will raise the IndentationError exception as shown below.
x=10 if x>100: #I don't know what to do. Will keep it empty. elif x>40: print("Pass") else: print("Fail")
File "/tmp/ipykernel_7853/2306930064.py", line 4 elif x>40: ^ IndentationError: expected an indented block after 'if' statement on line 2
In the above code, we don’t want to execute any statement if the value of x is greater than 100. So, we have just put a comment there. You can observe that the program runs into IndentationError as comments are ignored by the Python interpreter and it finds the if block empty.
To solve this issue, we can use the pass statement inside the if block. After this, the program will run smoothly without getting into any errors. You can observe this in the following example.
x=10 if x>100: #I don't know what to do. Will keep it empty. WIll use pass here pass elif x>40: print("Pass") else: print("Fail")
Here, we have used the pass statement inside the if block. Due to this, the code in the if block isn’t considered empty and the program works smoothly.
Create an Empty Function in Python
While implementing a program, we might not know the implementation of all the functions in our program. In such a case, if we define the function name and just put a comment there, the program will run into a SyntaxError exception. You can observe this in the following example.
def new_functions(a,b): #function will be implemented later
File "/tmp/ipykernel_7853/4234004695.py", line 2 #function will be implemented later ^ SyntaxError: incomplete input
Here, the comment inside the function is ignored. Due to this, the interpreter thinks that the function body is empty and it runs into an exception saying that there is incomplete input. To avoid this, you can use the pass statement as shown below.
def new_functions(a,b): #function will be implemented later pass
Create an Empty Class in Python
In a similar manner to functions, when we define a class without any method implementations or field declarations, the program will get into an error as shown below.
class new_class: #its a new class
File "/tmp/ipykernel_7853/3282010928.py", line 2 #its a new class ^ SyntaxError: incomplete input
Again, you can use the pass statement to implement an empty class definition in python as shown below.
class new_class: #its a new class pass
In a similar manner to the class definitions, you can also create empty methods inside a Python class using the pass keyword.
In this article, we discussed the basics of the Python pass statement. We also discussed how to implement empty classes and functions using the pass statement in Python. To learn more about Python programming, you can read this article on pandas map vs apply method in Python. You might also like this article on how to convert YAML to TOML format in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.