Python3 Comments
In Python, comments do not affect the execution of a program but make the code easier to read and understand.
Python has single-line comments and multi-line comments.
Single-line Comments
Section titled “Single-line Comments”In Python, single-line comments start with #. All text after the # symbol is treated as a comment and will not be executed by the interpreter.
Example
Section titled “Example”Multi-line Comments
Section titled “Multi-line Comments”In Python, multi-line strings (text blocks surrounded by three single quotes ''' or three double quotes """) can be used as multi-line comments.
1. Using Three Single Quotes
Section titled “1. Using Three Single Quotes”Example
Section titled “Example”2. Using Three Double Quotes
Section titled “2. Using Three Double Quotes”Example
Section titled “Example”Note: Although multi-line strings are used here as multi-line comments, they are actually strings. As long as we don’t use them, they won’t affect the program’s execution.
These strings can be placed in certain positions in the code without causing actual execution, thus achieving the effect of comments.
Multi-line Comment Notes
Section titled “Multi-line Comment Notes”In Python, multi-line comments are defined by three single quotes ''' or three double quotes """. This commenting method cannot be nested.
When you start a multi-line comment block, Python will treat all subsequent lines as comments until it encounters another set of three single quotes or three double quotes.
Nesting multi-line comments will cause a syntax error:
Example (Incorrect)
Section titled “Example (Incorrect)”In this example, the inner three single quotes are not correctly recognized as the end of the multi-line comment but are interpreted as a regular string, which will cause the code structure to be incorrect.
Correct approach: Use single-line comments for nesting
Example (Correct)
Section titled “Example (Correct)”Docstring
Section titled “Docstring”Python’s Docstring is a special type of comment used to add documentation to functions, classes, modules, etc. It is similar to Java’s Javadoc but more powerful and flexible.
Unlike regular comments, Docstrings can be accessed directly through the __doc__ attribute and can also be viewed using the help() function.
Basic Syntax
Section titled “Basic Syntax”Docstrings are enclosed in three double quotes """ or three single quotes ''' and placed at the beginning of functions, classes, or modules.
Example
Section titled “Example”Viewing Documentation with help()
Section titled “Viewing Documentation with help()”Example
Section titled “Example”Expected output:
Extracting Documentation with the inspect Module
Section titled “Extracting Documentation with the inspect Module”Python’s standard library provides the inspect module, which can directly extract documentation content:
Example
Section titled “Example”Expected output:
Multi-line Docstring
Section titled “Multi-line Docstring”For complex functions, you can use multi-line Docstrings:
Example
Section titled “Example”Expected output:
Class Docstring
Section titled “Class Docstring”Docstrings can also be used for classes:
Example
Section titled “Example”Expected output:
Docstring Conventions
Section titled “Docstring Conventions”There are several Docstring styles in Python, the most commonly used being:
- Google style: Uses space indentation with clear labels for parameters and return values.
- Sphinx/reST style: Uses a colon prefix, e.g.,
:param name:. - NumPy style: Similar to Google style but with slightly different formatting.
It is recommended to choose one style and remain consistent within a project.