Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

There is no advantage. If the function has a body at all, adding pass does nothing. It doesn't even do anything if there is no body, except to help the parser know that the empty body was intentional.

In real-world code, it's usually better to use a docstring than pass to denote deliberately-empty code:

class MyExtraException(Exception):
    """Just another exception"""

Is there any advantages?

No.

Placing pass at the end of non-empty code blocks like that just wastes lines. Remember that pass is a do-nothing placeholder in Python; it should only be used when you need to fill in an empty code block.

If you would like some examples of when to use pass, see this SO question as well as this one.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top