Python pass statement
What is pass statement in Python?
The pass
statement is used as a placeholder for future code.
The difference between a comment
and a pass
statement is that the Python interpreter will ignore a comment entirely and not ignore a pass
statement.
When a pass
statement is executed, nothing occurs, but you get the benefit of avoiding errors when empty code is not allowed.
In Python, empty code is not allowed in loops, if statements, function definitions, class definitions.
Syntax of pass statement
The syntax for a pass
statement in Python can be given as follows:
pass
pass Statement - Examples
In Python, the pass
statement is generally used as a placeholder.
For example, when you have a function
or a class
that is not implemented yet, but you want to implement it in the future. In Python, they cannot have an empty body because it will trigger errors. Here where the pass
statement came in hand to construct a body that does nothing.
In the following example, the pass
statement is used in the ìf
statement.
x = 36
y = 5454
if x < y:
pass
In the following example, the pass
statement is used in a function definition.
def myfunction():
pass
In the following example, the pass
statement is used in a class definition.
class User:
pass