Tuesday, 5 March 2019

Empty Function in Python

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():

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():

We can use pass in empty while statement also.
# Empty loop in Python
mutex = True
while (mutex == True) :

We can use pass in empty if else statements.
# Empty in if/else in Python
mutex = True
if (mutex == True) :
else :

1 comment:

Popular Posts


Android (21) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (4) C++ (81) Data Strucures (4) Downloads (1) Engineering (13) FPL (17) Hadoop (1) HTML&CSS (38) IS (25) Java (87) Leet Code (4) PHP (20) Python (214) R (69) Selenium Webdriver (2) Software (14) SQL (27)