tags:

views:

179

answers:

3

When writing code in Python, how can you write something next to it that explains what the code is doing, but which doesn't affect the code?

+17  A: 

I think you're talking about comments?

There are plain comments, which start with #:

return sys.stdin.readline()       # This is a comment

And also Docstrings, which document modules, classes, methods and functions:

def getline():
    """This is a docstring"""
    return sys.stdin.readline()

Unlike many other languages, Python does not have a multiline comment syntax (though docstrings can be multiline).

Greg
You might want to point out that you can't place triple-quoted strings anywhere you want. Docstrings can only exist at certain places in the code.
Bryan Oakley
-1 for not making clear the important distinction between comments (how you do something) and docstrings (what the code is supposed to do).
nikow
@nikow: I can't understand the distinction you're making between how the code works and what the code does.
S.Lott
@S.Lott: I assume your personal statement is meant as a question? Comments should describe implementation details, docstrings describe the API (at least in an ideal world with non-leaky abstractions). Just describing the docstring syntax without any info on how to use them proberly is probably not very helpful for the OP.
nikow
@Bryan: You can place strings, quoted in a way you prefer, almost anywhere.
kaizer.se
@kaizer: You are not seriously suggesting to use string expressions instead of comments?
nikow
@nikow: I was primarily correcting something that was outright false. Triple-quoted strings provide multi-line "comment" function for quickly commenting out code, so yes, I've used that, but more permanent comments are usually written with comment syntax.
kaizer.se
+2  A: 

Write a comment? Python comments start with #.

ndim
+2  A: 

You mean comments? Use the # character before your comment.

http://en.wikibooks.org/wiki/Python%5FProgramming/Source%5FDocumentation%5Fand%5FComments

# This is a comment
print("Hello comment!")
Marty Dill