3

When I need to write extensive comments about functions, I use docstrings. However, I'm not sure what the equivalent for file level comments is in Python, and whether it differs for modules vs scripts.

Is it common to use this style?

"""
file.py: module for X
Detailed information...
"""

import x

def foo(bar):
  return 42

Or perhaps this?

# file.py: module for X
# More info...

import x
# ...

Any thoughts?

1
  • 2
    Always use the first, since that will actually populate __doc__, the other one is just a comment. Commented May 22, 2021 at 12:21

1 Answer 1

6

Always use a string, since that will actually populate the __doc__ variable of the current file / module.

Compare this:

'''Hello World'''
print(__doc__)

to

# Hello World
print(__doc__)

Consider this is in a module foo.py and compare the output off:

>>> import foo
>>> help(foo)

You also do not need to add the "file: ..." part, since python displays that automatically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.