Python Pass: How to Use Pass Statement with Example
Python pass is an inbuilt statement that is used as the placeholder for future implementation of functions, loops, etc. The pass is the null statement. The difference between a comment and the pass statement in Python is that, while the interpreter ignores the comment entirely, the pass statement is not ignored.
Python Pass Example
It is used when the statement is required syntactically, but you do not want that code to execute. The pass statement is the null operation; nothing happens when it runs.
The pass statement is also useful in scenarios where your code will eventually go but has not been entirely written yet.
The syntax of the pass is following.
Let’s see the example of the pass statement in python.
# app.py listA = [18, 21, 19, 29, 46] for val in listA: pass
The above code will not do anything, but it also does not throw any error. So that is the beauty of the pass statement.
Let’s see other examples with the class and functions in Python.
def function(args): pass
Here, if you call the function, then you will not get any value or error, and you can do the same with a class.
class App: pass
The most use case of the pass statement is the scenario where you are designing a new class with some methods that you do not want to implement, yet.
Python Language has the syntactical requirement that code blocks like if, except, def, class, etc. cannot be empty. Empty code blocks are however useful in the variety of different contexts in the program.
Therefore, if nothing is supposed to happen in the code, the pass statement is needed for that block not to produce the IndentationError.
Alternatively, any statement (including just a term to be evaluated, like the Ellipsis literal … or a string, most often a docstring) can be used, but the pass statement makes clear that indeed nothing is supposed to happen in that block of code, and does not need to be actually run and (at least temporarily) stored in the memory.
Finally, Python Pass Statement Example is over. Thanks for taking it.