Python pass keyword
Python pass
Python pass keyword acts as a no-operation statement. It can be used as a placeholder for implementation of a loop body, function body, or something like that, in the places where you need a statement, but does nothing at all.
When you compare it to other programming languages like Java, C or C++, you may notice that you can leave the body of a function, class or a loop in closed parenthesis. Following is a Java function with empty body.
public static void printMessage() {
}
But in Python, a loop, function or a class cannot have its body empty. Following is not valid in Python.
def printMessage():
#nothing
So, pass keyword comes to help. You can have an empty function, that does nothing, with pass as only statement for the function body.
def printMessage():
pass
Python Pass statement in For Loop
Following example demonstrates the usage of pass to define an empty for loop, that does nothing.
for x in range(10) :
pass
Python Pass statement in Class
Following example demonstrates the usage of pass to define an empty for loop, that does nothing.
class Student:
pass
Python pass vs Python comment
The main difference between pass and comments is in terms of how Python Interpreter considers these statements.
Python Interpreter ignores a comment for execution, but does not ignore a pass. Python Interpreter executes pass statement just like any other statement, but the difference is that pass does nothing.
So, you can consider that Python pass is a dummy statement, which does nothing, but can fill for a Python statement when needed.
Python pass vs Python continue statements
The difference between pass and continue statements is based on the jumping of program execution after its execution.
pass passes the execution to next statement.
continue passes the execution to next iteration of loop.
Summary
In this Python Tutorial, we learned what pass is, and how we can use it for creating an empty loop, function or class bodies.