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
2009-10-23 15:26:36
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
2009-10-23 15:34:01
-1 for not making clear the important distinction between comments (how you do something) and docstrings (what the code is supposed to do).
nikow
2009-10-23 15:44:28
@nikow: I can't understand the distinction you're making between how the code works and what the code does.
S.Lott
2009-10-23 16:25:57
@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
2009-10-23 17:50:35
@Bryan: You can place strings, quoted in a way you prefer, almost anywhere.
kaizer.se
2009-10-23 19:41:55
@kaizer: You are not seriously suggesting to use string expressions instead of comments?
nikow
2009-10-23 19:56:56
@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
2009-10-27 16:24:33
+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
2009-10-23 15:28:00