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:
- Loops and Control Statements (continue, break and pass) in Python
- with statement in Python
- Using else conditional statement with for loop in python
- Statement, Indentation and Comment in Python
- numpy.empty() in Python
- Python | Check whether a list is empty or not
- Python | Pandas TimedeltaIndex.empty
- Python program to check if string is empty or not
- Python | Initializing dictionary with empty lists
- Python | Remove and print every third from list until it becomes empty
- Python | Remove empty tuples from a list
- Write your own len() in Python
- Python | Create and write on excel file using xlsxwriter module
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Remove empty strings from list of strings



