tags:

views:

278

answers:

7

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

#########################################################
# Create a new user
#########################################################
def add(self):
+2  A: 

Read about using docstrings in your python code.

As per the Python Docstring Conventions:

The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated. It should be documented whether keyword arguments are part of the interface.

There will be no golden rule, but rather provide comments that mean something to the other developers on your team (if you have one) or even to yourself when you come back to it six months down the road.

Mat Nadrofsky
+23  A: 

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.

See: http://diveintopython.org/getting_to_know_python/documenting_functions.html

Chinmay Kanchi
Note that it doesn't have to be triple-quoted; any string literal will work. But you can put more information in a multiline string.
Ignacio Vazquez-Abrams
Though convention dictates that it should be triple-quoted. I've never seen a docstring that wasn't.
Chinmay Kanchi
@Chinmay: Here's one! http://code.djangoproject.com/browser/django/trunk/django/contrib/gis/db/models/fields.py#L42
jcdyer
Which is not to say that I don't agree. They should be triple quoted, but you will see some in the wild that aren't.
jcdyer
You can also use three single-quotes (rather than three double-quotes) to open and close the docstring.
Craig McQueen
+3  A: 

I would go for a documentation practice that integrates with a Documentation tool such as Sphinx.

The first step is to use a docstring:

def add(self):
 """ Method which adds stuff
 """
jldupont
+8  A: 

Use a docstring.

Deniz Dogan
Read PEP 257: http://www.python.org/dev/peps/pep-0257/
S.Lott
+5  A: 

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.

Tim Pietzcker
+1 for doctest!
Mat Nadrofsky
Yes, doctest is awesome :)
Chinmay Kanchi
+1  A: 

Oh boy! Consider a can of worms opened :)

The principles of good commenting are fairly subjective, but here are some guidlines:

  • 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, Javadoc in Java
Dancrumb
there is nothing subjective about this, Python is very clear about using Docstring commenting.
fuzzy lollipop
@fuzzy lollipop, I appreciate the comment, but you'll note that my last point makes that exact point. Perhaps the OP's question is only about the mechanics of commenting in Python, but I don't think my answer warrants down-voting
Dancrumb
I agree. +1 :))
Tim Pietzcker
+1  A: 

I would go a step further than just saying "use a docstring". Pick a documentation generation tool, such as pydoc or epydoc (I use epydoc in pyparsing), and use the markup syntax recognized by that tool. Run that tool often while you are doing your development, to identify holes in your documentation. In fact, you might even benefit from writing the docstrings for the members of a class before implementing the class.

Paul McGuire