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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Incorrect empty function in Python
def fun(): 

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Correct way of writing empty function 
# in Python
def fun(): 
    pass

chevron_right


We can use pass in empty while statement also.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Empty loop in Python
mutex = True
while (mutex == True) :
    pass

chevron_right


We can use pass in empty if else statements.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Empty in if/else in Python
mutex = True
if (mutex == True) :
    pass
else :
    print("False")

chevron_right


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



My Personal Notes arrow_drop_up


Article Tags :

7


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.