Ruggero Turra Ruggero Turra - 7 months ago 8
Python Question

pass keyword to mark end room

I have recently noticed that some people are used to mark the end of rooms with the pass keyworkd as

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


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

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

Answer

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"""
Comments