FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Pass Statement




The pass statement acts as a placeholder and usually used when there is no need of code but a statement is still required to make a code syntactically correct. For example we want to declare a function in our code but we want to implement that function in future, which means we are not yet ready to write the body of the function. In this case we cannot leave the body of function empty as this would raise error because it is syntactically incorrect, in such cases we can use pass statement which does nothing but makes the code syntactically correct.

Pass statement vs comment

You may be wondering that a python comment works similar to the pass statement as it does nothing so we can use comment in place of pass statement. Well, it is not the case, a comment is not a placeholder and it is completely ignored by the Python interpreter while on the other hand pass is not ignored by interpreter, it says the interpreter to do nothing.

Python pass statement example

If the number is even we are doing nothing and if it is odd then we are displaying the number.

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)







Leave Comment