python 注释含注释_用Python阻止注释
python 注释含注释
注释 (Comments)
A comment is a piece of text in a computer program that is meant to be a programmer-readable explanation or annotation in the source code and is ignored by compiler/interpreter.
注释是计算机程序中的一段文本,旨在作为源代码中的程序员可读的解释或注释,并且被编译器/解释器忽略。
In simpler words, as we start adding more functionality and features to our program the size of code increases! It might happen that when we return to it after a few months we might not be able to understand it and get confused!
简而言之,随着我们开始向程序中添加更多功能和特性,代码的大小也随之增加! 可能会发生,几个月后返回时,我们可能无法理解它并感到困惑!
Thus adding comments in the program is one of the most important hygiene factors. It is important to make sure that your code can be easily understood by others and you (even when you revisit after a few months). Often text editors highlight comments differently so they can be easily identified.
因此,在程序中添加注释是最重要的卫生因素之一。 确保您的代码可以被他人和您轻松理解(即使几个月后重新访问)也很重要。 文本编辑器通常以不同的方式突出显示注释,因此可以轻松识别它们。
Usually, the code only tells you how it does but cannot tell you why it does so?
通常,代码仅告诉您它是如何执行的,而不能告诉您为什么这样做?
Sometimes our variables are also not named very specifically. However, comments in python can help us get better clarity. We use comments to explain the formulas and the actual logic behind the particular step/algorithm.
有时,我们的变量也没有特别命名。 但是, python中的注释可以帮助我们获得更好的清晰度。 我们使用注释来解释特定步骤/算法背后的公式和实际逻辑。
Python注释类型 (Type of Python Comments)
Python can have both Block Comments and Inline Comments,
Python可以同时具有块注释和内联注释 ,
1)块注释 (1) Block Comments)
Block comments apply to the piece of code that it follows. It might apply to a portion of code or the entire code. They are indented to the same level as that code. Each line of comment starts with a #.
块注释适用于其后的代码段。 它可能适用于部分代码或整个代码。 它们缩进到与该代码相同的级别。 每行注释都以#开头。
# Python program to print
# Hello World
print("Hello World")
Output:
输出:
Hello World
Block comments can also be made using ‘’’ ‘’’. Anything written between these quotes is considered as comment.
块注释也可以使用‘’’’’’进行 。 这些引号之间写的任何内容均视为注释。
'''
Python program to print
Hello World
'''
print("Hello World")
Output:
输出:
Hello World
Unless it is used right after the definition of a function, method, class, or module. In this case, it becomes a docstring.
除非在定义函数,方法,类或模块之后立即使用它。 在这种情况下,它成为docstring 。
def hello():
'''
Here, this is docstring
since it is written right after
the function definition.
below given print statement will print
Hello World
'''
print("Hello World")
if __name__ == "__main__":
# calling hello function
hello()
Output:
输出:
Hello World
2)内联评论 (2) Inline Comments)
An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.
内联注释是与语句在同一行上的注释。 内联注释应与语句至少分隔两个空格。 它们应以#和单个空格开头。
print("Hello World") # This is an inline comment
Output:
输出:
Hello World
翻译自: https://www.includehelp.com/python/block-comments.aspx
python 注释含注释
还没有评论,来说两句吧...