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:
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?
- How to split a string in C/C++, Python and Java?



