Ruggero Turra Ruggero Turra - 6 months ago 5x
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):

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

I know
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.


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