Search code examples
pythoncoding-style

`pass` keyword to mark end of indented code block


I have recently noticed that some people mark the end of indented code blocks with the pass keyword, such as:

def f():
    for i in range(10):
        do_something(i)
        pass
    pass

I don't understand why. Are there any advantages?

I know the pass keyword does nothing and it is the equivalent of { } in other languages. My question is more related to a convention. For example, maybe people like to have something marking the end of a long code block.


Solution

  • Do you think Python's indention-sensitive grammar is harmful?

    If your answer is "no", then using pass to mark an end does nothing but clutter the cleanness brought in by Python's indention sensitive grammar.

    If your answer is "yes", then using pass to mark an end is a workaround for the ambiguity brought in by Python's indention sensitive grammar.

    In Python, there is no {} and end, but semantic white spaces.

    For example, consider the following Ruby code:

    def f
        for i in 0...10
            i = i * 2
            print(i)
        end
    end
    

    And the equivalent Python code:

    def f():
        for i in range(10):
            i = i * 2
            print(i)
    

    With one wrong keystroke (TAB):

    def f():
        for i in range(10):
            i = i * 2
        print(i)
    

    The above code is also valid.

    To avoid this kind of mistakes, we can use pass for end in Python:

    def f():
        for i in range(10):
            i = i * 2
            print(i)
            pass
        pass
    

    With one wrong keystroke (TAB):

    def f():
        for i in range(10):
            i = i * 2
        print(i)
            pass
        pass
    

    Python will refuse to work:

    IndentationError: unexpected indent
    

    However, python will not always catch unintended indentation even with pass.

    Suppose we intend to write:

    def g():
        for i in range(10):
            i = i * 2
            pass
        print(i)
        pass
    

    With one wrong keystroke (TAB):

    def g():
        for i in range(10):
            i = i * 2
            pass
            print(i)
        pass
    

    Python will not refuse to work.

    But using pass still has two advantages:

    • It still provides visual hint.

      If you think pass as the last clause of an indented block, print(i) looks weird to you in the above code.

    • A decent editor/IDE will indent correctly if you typed pass.

    For the second pass, if f() is a top-level function, and your code adheres to PEP8:

    Surround top-level function and class definitions with two blank lines.

    You may omit the second pass.

    But PEP8 also said:

    Extra blank lines may be used (sparingly) to separate groups of related functions.

    Thus I prefer pass.