How to write an empty function in Python – pass statement?
In C/C++ and Java, we can write empty function as following
// An empty function in C/C++/Java
void fun() { }
In Python, if we write something like following in Python, it would produce compiler error.
# Incorrect empty function in Python def fun(): |
chevron_right
filter_none
Output :
IndentationError: expected an indented block
In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement.
# Correct way of writing empty function # in Python def fun(): pass |
chevron_right
filter_none
We can use pass in empty while statement also.
# Empty loop in Python mutex = Truewhile (mutex == True) : pass |
chevron_right
filter_none
We can use pass in empty if else statements.
# Empty in if/else in Python mutex = Trueif (mutex == True) : passelse : print("False") |
chevron_right
filter_none
This article is contributed by Shivam Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- break, continue and pass in Python
- Difference between continue and pass statements in Python
- Python | Split and Pass list as separate parameter
- Loops and Control Statements (continue, break and pass) in Python
- with statement in Python
- Python break statement
- Python Continue Statement
- Python return statement
- Using else conditional statement with for loop in python
- Statement, Indentation and Comment in Python
- numpy.empty() in Python
- Create an empty file using Python
- Python: Check if a directory is empty
- Python | Check if dictionary is empty
- Python | Pandas DataFrame.empty


