Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic file level comments in Python?

Tags:

python

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?

like image 513
Oliver Avatar asked Sep 20 '26 01:09

Oliver


1 Answers

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.

like image 87
MaxNoe Avatar answered Sep 21 '26 14:09

MaxNoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!