What is the proper way to comment functions in python?


Question

Is there a generally accepted way to do this? Is this acceptable:

#########################################################
# Create a new user
#########################################################
def add(self):
1
153
3/1/2010 4:21:13 PM

Accepted Answer

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

281
6/30/2016 4:22:59 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon