Python3 Comments
In Python, comments do not affect program execution, but they make code easier to read and understand.
Comments in Python includeSingle-line commentandMulti-line comment。
Single-line comment
Single-line comments in Python start with #, and# all text after the symbol is treated as a comment and is not executed by the interpreter.
Example
This is a comment
(
"Hello, World!"
)
This is also a comment
Multi-line comment
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
Example
#!/usr/bin/python3
'''
This is a multi-line comment using three single quotes
This is a multi-line comment using three single quotes
This is a multi-line comment using three single quotes
'''
(
"Hello, World!"
)
2. Using three double quotes
Example
#!/usr/bin/python3
"""
This is a multi-line comment using three double quotes
This is a multi-line comment using three double quotes
This is a multi-line comment using three double quotes
"""
(
"Hello, World!"
)
Note: although multi-line strings are used as multi-line comments here, they are actually strings; as long as we do not use them, they do not affect the running of the program.
These strings can be placed in certain positions in the code without causing actual execution, thus achieving the effect of comments.
Notes on multi-line comments
In Python, multi-line comments are defined by three single quotes ''' or three double quotes """,this comment style cannot be nested。
When you start a multi-line comment block, Python treats all subsequent lines as comments until it encounters another set of three single or three double quotes.
Nested multi-line comments cause a syntax error:
Example (incorrect)
'''
This is the outer multi-line comment
It can contain some descriptive content
'''
This is an attempt at a nested multi-line comment
Will cause a syntax error
'''
'''
In this example, the inner three single quotes are not correctly recognized as the end of the multi-line comment, but are interpreted as an ordinary string, which makes the code structure incorrect.
Correct approach: nest single-line comments
Example (correct)
'''
This is the outer multi-line comment
It can contain some descriptive content
This is an inner single-line comment
It can be nested inside a multi-line comment
'''
Docstring documentation strings
Python's Docstring is a special kind of comment used to add documentation to functions, classes, modules, and so on. It is similar to Java's Javadoc but more powerful and flexible.
Unlike ordinary comments,a Docstring can be accessed directly through the __doc__ attribute, or viewed with the help() function.
Basic Syntax
A Docstring is surrounded by three double quotes """ or three single quotes ''' and placed at the beginning of a function, class, or module.
Example
def
add
(
a
,
b
)
:
"""Return the sum of two numbers"""
return
a + b
Pass doc Attribute access
(
add.doc
)
Output: Return the sum of two numbers
View documentation with help()
Example
def
add
(
a
,
b
)
:
"""Return the sum of two numbers"""
return
a + b
Use the help() function
help
(
add
)
Expected output:
Help on function add in module __main__:
add(a, b)
返回两数之和
Extract documentation with the inspect module
Python's standard library provides the inspect module, which can extract documentation content directly:
Example
import
inspect
def
add
(
a
,
b
)
:
"""Return the sum of two numbers"""
return
a + b
Get documentation with inspect.getdoc()
(
inspect
.
getdoc
(
add
)
)
Output: Return the sum of two numbers
Expected output:
返回两数之和
Multi-line Docstrings
For complex functions, you can use a multi-line Docstring:
Example
def
calculate
(
a
,
b
,
operation
=
"add"
)
:
"""
Perform a mathematical operation
Parameters:
a: the first number
b: the second number
operation: the operation type, one of "add", "subtract", "multiply"
Returns:
The computed result
"""
if
operation
==
"add"
:
return
a + b
elif
operation
==
"subtract"
:
return
a - b
elif
operation
==
"multiply"
:
return
a * b
else
:
raise
ValueError
(
"Unsupported operation"
)
View the full documentation
help
(
calculate
)
Expected output:
Help on function calculate in module __main__:
calculate(a, b, operation='add')
执行数学运算
参数:
a: 第一个数字
b: 第二个数字
operation: 操作类型,可选 "add", "subtract", "multiply"
返回:
计算结果
Class Docstrings
Docstrings can also be used for classes:
Example
class
Person:
"""A Person class representing a person's basic information"""
def
init
(
self
,
name
,
age
)
:
"""
Initialize the Person object
Parameters:
name: the person's name
age: the person's age
"""
self
.
name
=
name
self
.
age
=
age
def
introduce
(
self
)
:
"""Introduce this person"""
return
f
"My name is {self.name} and I am {self.age} years old"
Access the class's documentation
(
Person.doc
)
Access the method's documentation
(
Person.
introduce
.doc
)
Expected output:
人物类,用于表示一个人的基本信息
介绍这个人
Docstring conventions
There are several Docstring styles in Python; the common ones are:
- Google style: uses space indentation, with explicit labels for parameters and return values.
- Sphinx/reST style: starts with a colon, such as
:param name:。 - NumPy style: similar to Google style, but with a slightly different format.
It is recommended to choose one style in a project and stay consistent.