Topic: Python pass Statement Example Code
The pass statement in Python With Example Programs
The Python pass statement indicates a null operation. Nothing happens, when the Python interpretor executes a pass statement. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.
class MyClass: pass # a class with no methods (yet) , may be provided later or class MyClass: pass # empty statement, yet, no class features
Note: we can use pass statent on the same line or on next line as indented empty statement block.
In the above example, we declare an empty class named MyClass. Note that, a class definition may not be empty, we must provide at least a pass statement.
Why to use pass statement? Why not to use a Comment?
The main difference between a pass statement and a comment is that: In Python a pass statement is executable, although it asks the interpretor to do nothing. Weheras the comment is ignorable and is not executed. Hence we cannot use a comment in place of a pass statement in definition of an empty class.
For example, if we use a comment to indicate an empty class, there will be an error message as shown below:
class MyEmptyClass2: # empty class yet Output: Syntax Error
What is the Puropse of Python pass statement?
The purpose of the Python pass statement is that pass represents an empty statement or null statement in Python code. We can use pass statement to indicate – “Do nothing”.
The pass statement in Python represents empty statement, empty functions, empty classes and empty loops.
Actually, we may use the pass statement if we are not prepared to give implimentation details for a function or class. therefore we will use pass statement for the time being and provide the complete implimentation details later.
Python pass Statement Example Code: a brief Tutorial
Output: Python pass statement example Program: Do Nothing Program Started Program Ended You will also Like Perfect Python Concepts: