Signup/Sign In
LAST UPDATED: MARCH 15, 2021

Python pass Statement

Technology #Python#Python pass

    What do you usually do when you write code, but plan to write the logic for a specific function later? People would usually put in a comment and move ahead with writing rest of the code.

    There is no problem in doing so. But what if I tell you that there is something else that behaves just like a comment but gets executed like regular code in Python and has no meaning?

    Yes, you read that right. The pass statement. It is a null statement that is interpreted by Python as a piece of code which has no special meaning. It behaves as a placeholder in places where functions and other logic would be implemented later on. After executing the pass statement, it results in a no-operation (also known as NOP). This statement doesn't take any parameters.

    Syntax of pass Statement

    It has a very simple syntax,

    pass

    We can't really leave the future implementation of methods or logic empty since this would throw an error when executed. Because of this, the pass statement comes in handy.

    Time for some examples:

    Let's see a few code examples to understand the use of the pass statement,

    Using pass with data structures:

    my_sequence = ['s', 't', 'u', 'dytonight']
    
    for value in my_sequence:
        pass

    The above code will have nothing in the output.

    Using pass with functions:

    def func(arguments):
        pass
    
    func(1)

    Again, the function defined in the code above, does nothing.

    Using pass with classes:

    class studytonight:
        pass
    

    Output:

    No output can be seen after executing all the 3 example pieces of code since no operation takes place post-execution of the above code.

    Similarly, the pass statement can also be used in if-else blocks where we do not want to perform any action.

    Conclusion

    In this post, we saw how a pass statement can be used inside a Python program, without disturbing other parts of code and letting the Python interpreter execute the code which hasn't been implemented as well.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS