
Comments#注释
Comments are annotations to code used to make it easier to understand. They don't affect how code is run.
In Python, a comment is created by inserting an octothorpe (otherwise known as a number sign or hash symbol: #). All text after it on that line is ignored.
For example:
x = 365
y = 7
# this is a comment
print(x % y) # find the remainder
# print (x // y)
# another comment
>>>
1
>>>
注释是代码的注释,用于使代码更容易理解。它们不影响代码的运行方式。
在Python中,通过插入一个octhorpe(也称为数字符号或散列符号:#)来创建注释。这一行后面的所有文本都将被忽略。
例如:
x = 365
y = 7
# this is a comment
print(x % y) # find the remainder
# print (x // y)
# another comment
>>>
1
>>>
Python doesn't have general purpose multiline comments, as do programming languages such as C.
Python没有通用的多行注释,C语言等编程语言也没有。
Docstrings#文档字符串
Docstrings (documentation strings) serve a similar purpose to comments, as they are designed to explain code. However, they are more specific and have a different syntax. They are created by putting a multiline string containing an explanation of the function below the function's first line.
def shout(word):
"""
Print a word with an
exclamation mark following it.
"""
print(word + "!")
shout("spam")
Result:
>>>
spam!
>>>
docstring(文档字符串)的作用类似于注释,因为它们的目的是解释代码。但是,它们更具体,语法也不同。它们是通过将包含函数解释的多行字符串放在函数的第一行下面来创建的。
def shout(word):
"""
Print a word with an
exclamation mark following it.
"""
print(word + "!")
shout("spam")
Result:
>>>
spam!
>>>
Unlike conventional comments, docstrings are retained throughout the runtime of the program. This allows the programmer to inspect these comments at run time.
与传统的注释不同,文档字符串在程序的整个运行期间都保持不变。这允许程序员在运行时检查这些注释。