Let's say, I've got a function like this:
def myFunc():
# useful function to calculate stuff
This will produce an indentation error, unless I add pass
:
def myFunc():
# useful function to calculate stuff
pass
However, if I replace a comment with docstring, no pass
is necessary:
def myFunc():
"""useful function to calculate stuff"""
This seems like an odd feature as neither of these are used in the program, as far as I know. So, why does it behave like this?
A docstring isn't just a comment. It actually has meaning to the interpreter. In the case with a docstring, you could do myFunc.__doc__
and actually get your docstring back (In the other case with a pass
, the result myFunc.__doc__
would be None
).
In other words, you are actually adding some code to the function body to modify it's behavior (in some circumstances), so no pass
is necessary.