What is the proper way to comment functions in Python?
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream
--
Chapters
00:00 What Is The Proper Way To Comment Functions In Python?
00:18 Accepted Answer Score 469
00:49 Answer 2 Score 21
02:08 Answer 3 Score 30
02:24 Answer 4 Score 13
03:03 Thank you
--
Full question
https://stackoverflow.com/questions/2357...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ACCEPTED ANSWER
Score 469
The correct way to do it is to provide a docstring. That way, help(add) will also spit out your comment.
def add(self):
    """Create a new user.
    Line 2 of comment...
    And so on... 
    """
That's three double quotes to open the comment and another three double quotes to end it. You can also use any valid Python string. It doesn't need to be multiline and double quotes can be replaced by single quotes.
See: PEP 257
ANSWER 2
Score 30
Use a docstring, as others have already written.
You can even go one step further and add a doctest to your docstring, making automated testing of your functions a snap.
ANSWER 3
Score 21
Use a docstring:
A string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the
__doc__special attribute of that object.All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the
__init__constructor) should also have docstrings. A package may be documented in the module docstring of the__init__.pyfile in the package directory.String literals occurring elsewhere in Python code may also act as documentation. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes (i.e. not assigned to
__doc__), but two types of extra docstrings may be extracted by software tools:
- String literals occurring immediately after a simple assignment at the top level of a module, class, or
 __init__method are called "attribute docstrings".- String literals occurring immediately after another docstring are called "additional docstrings".
 Please see PEP 258 , "Docutils Design Specification" [2] , for a detailed description of attribute and additional docstrings...
ANSWER 4
Score 13
The principles of good commenting are fairly subjective, but here are some guidelines:
- Function comments should describe the intent of a function, not the implementation
 - Outline any assumptions that your function makes with regards to system state. If it uses any global variables (tsk, tsk), list those.
 - Watch out for excessive ASCII art. Having long strings of hashes may seem to make the comments easier to read, but they can be annoying to deal with when comments change
 - Take advantage of language features that provide 'auto documentation', i.e., docstrings in Python, POD in Perl, and Javadoc in Java