📜  关于Python多行注释的有趣事实

📅  最后修改于: 2022-05-13 01:55:52.406000             🧑  作者: Mango

关于Python多行注释的有趣事实

多行注释(注释块)用于描述大文本代码或在调试应用程序时注释掉代码块。
Python是否支持多行注释(如 c/c++...)?
实际上,在许多在线教程和网站中,您会发现 multiline_comments 在Python中可用(“””或“')。但让我们先来看看Python的创造者“Guido van Rossum”是怎么说假块评论风格的:
Python提示:您可以使用多行字符串作为多行注释。除非用作文档字符串,否则它们不会生成任何代码!。
源文件

  • 如果在单行中编写文本,则在Python中使用双引号或单引号。
  • 如果写一首诗、歌曲或多行文本,则使用三引号(“””或“')。
  • 如果打印这些行或文本,则只需将其插入 print()函数。

例子:

Python3
# Write Python3 code here
#this is only valid for single line
  
"this is a text constant for single line you"
"can't use multi-line within single or double quotes"
 
'this is also text-constant for single line'
  
#this is valid for multiline.
"""this is text constant for multi
or single line this will not
give any error"""
  
#you can also print both look at below..
print('this is also text-constant for single line')
print("""this is text constant for mult-line
or single line this will not
give any error""")


Python3
# Write Python3 code here
def check_syntax():
    """
    look at your first ident
    below"""
print("after this line interpreter gives error because your first ident doesnot match")
 """ <---- here first ident starts with 2nd column hence gives indentation error you must remember that  your first ident must be correctly  matched.
"""


上述技术不会创建真实的评论。它只是插入不会改变任何内容的 Text 常量,或者说这是代码中某处的常规单行字符串。始终记住要正确缩进第一个( “””或“' )匹配项,否则会生成 SyntaxError。
例子:

Python3

# Write Python3 code here
def check_syntax():
    """
    look at your first ident
    below"""
print("after this line interpreter gives error because your first ident doesnot match")
 """ <---- here first ident starts with 2nd column hence gives indentation error you must remember that  your first ident must be correctly  matched.
"""

我们如何轻松注释掉代码块?
只需选择这些行(这意味着何时复制文本然后首先选择这些行)然后按 (cntrl+#) 否则每行中连续的 # 是唯一的方法。
概括:

  • 与其他编程语言(c、c++、...)不同,“Python”不支持开箱即用的多行注释块
  • 可以将三引号“””视为多行注释,但这不是一个好主意,因为这种类型的注释行可能会变成意外的文档字符串。
  • 连续 # 是在Python中注释行的唯一方法(如果您想注释多行,则可以选择这些行并在 python3.0 中按 (ctrl+#)。