Python

Python Pass Message – Detailed Instructions From [A-Z]

The pass statement serves as a stand-in and is typically used when no actual code is needed but a statement is still needed to ensure that the code is syntactically correct. For instance, we may want to declare a function in our code but later implement it, which indicates we are not yet prepared to write the function’s body. Because the code is syntactically incorrect, we cannot leave the body of the function empty in this situation. Instead, we can use the pass statement, which accomplishes nothing but makes the code correct.

Statement versus comment

You might be wondering how a Python comment compares to a pass statement because it has the same effect of doing nothing and can therefore be used in place of the pass statement. The Python interpreter entirely ignores a comment since it is not a placeholder, whereas a pass instructs the interpreter to disregard the comment instead of doing nothing.

Example of pass statement in Python

If the number is even, nothing is done; if it is odd, the number is displayed.

for num in [20, 11, 9, 66, 4, 89, 44]:
    if num%2 == 0:
        pass
    else:
        print(num)

Output:

11
9
89

Other examples:

A function that does nothing(yet), may be implemented in future.

def f(arg): pass    # a function that does nothing (yet)

A class that does not have any methods(yet), may have methods in future implementation.

class C: pass       # a class with no methods (yet)

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button