Is there a generally accepted way to do this? Is this acceptable:
#########################################################
# Create a new user
#########################################################
def add(self):
Is there a generally accepted way to do this? Is this acceptable:
#########################################################
# Create a new user
#########################################################
def add(self):
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.
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
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.
Oh boy! Consider a can of worms opened :)
The principles of good commenting are fairly subjective, but here are some guidlines:
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.