-
Notifications
You must be signed in to change notification settings - Fork 2
Python Comments
Comments are used to annotate, describe, or explain code that is complex or difficult to understand. Python will intentionally ignore comments when it compiles to bytecode by the interpreter. PEP 8
has a section dealing with comments.
Block and inline comments start with a #
, followed by a space before the comment:
# This is a block comment.
print('Hello world!') # This is an inline commment.
Python does not include a formal way to write multiline comments. Each line of a comment spanning multiple lines should start with #
and a space:
# This is the first line of a multiline comment.
# This is the second line.
Another type of comment is the docstring, documented in PEP 257
. Docstrings are a specific type of comment that becomes the __doc__
attribute.
For a string literal to be a docstring, it must start and end with \"\"\"
and be the first statement of the module, function, class, or method definition it is documenting:
class SomeClass():
"""Summary line for SomeClass.
More elaborate descriptions may require using a
a multiline docstring.
"""
def method_a(self):
"""Single line summary of method_a."""
pass
String literals that start and end with """
that are not docstrings (not the first statement), can be used for multiline strings. They will not become __doc__
attributes. If they are not assigned to a variable, they will not generate bytecode. There is some discussion about using them as multiline comments found here.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links