Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法在Python中创建多行注释?_Python_Comments_Documentation - Fatal编程技术网

有没有办法在Python中创建多行注释?

有没有办法在Python中创建多行注释?,python,comments,documentation,Python,Comments,Documentation,我最近开始学习,但找不到如何实现多行注释。大多数语言都有块注释符号,如 /* */ 我在Python中尝试了这个方法,但是它抛出了一个错误,所以这可能不是正确的方法。Python真的有多行注释功能吗?我认为它没有,只是没有处理多行字符串。然而,大多数(如果不是所有的话)Python IDE都有一个用于“注释”多行代码的快捷键。Python确实有一个快捷键,除非用作docstring,--就像-预先注释一样。实际上,它的行为与评论一模一样 另一方面,如果您说必须在官方文档中记录此行为才能成为真

我最近开始学习,但找不到如何实现多行注释。大多数语言都有块注释符号,如

/*

*/

我在Python中尝试了这个方法,但是它抛出了一个错误,所以这可能不是正确的方法。Python真的有多行注释功能吗?

我认为它没有,只是没有处理多行字符串。然而,大多数(如果不是所有的话)Python IDE都有一个用于“注释”多行代码的快捷键。

Python确实有一个快捷键,除非用作docstring,--就像
-预先注释一样。实际上,它的行为与评论一模一样

另一方面,如果您说必须在官方文档中记录此行为才能成为真正的注释语法,那么是的,您可以说它不保证作为语言规范的一部分

在任何情况下,您的文本编辑器也应该能够轻松地注释出所选区域(通过在每行前面分别放置一个
#
)。如果没有,请切换到有此功能的文本编辑器

在没有特定文本编辑功能的Python中编程可能是一种痛苦的经历。找到合适的编辑器(并知道如何使用它)可以极大地改变人们对Python编程体验的看法

文本编辑器不仅能够注释选定的区域,还应该能够轻松地将代码块左右移动,并且当您按Enter键时,它应该自动将光标放置在当前缩进级别。代码折叠也很有用


为防止链路衰减,以下是:

Python提示:可以将多行字符串用作多行注释。除非用作docstring,否则它们不会生成代码!:-)


好了,Python没有块注释。对于注释各行,可以使用
#
字符

hello = "Hello!" # This is an inline comment
print(hello)
dial = "Dial #100 to make an emergency call."
print(dial)

如果您正在使用。我确信其他人喜欢并具有类似的功能。

您可以使用三重引号字符串。当它们不是docstring(类/函数/模块中的第一件事)时,它们将被忽略

'''
This is a multiline
comment.
'''
(确保适当缩进前导的
'
,以避免出现
缩进错误

(Python的创建者)作为“专业提示”

但是,Python的样式指南PEP8如下所示:

# This is a multiline
# comment.

…这也是您在许多项目中可以找到的。文本编辑器通常有一个快捷方式来轻松完成此操作。

在Python 2.7中,多行注释是:

"""
This is a
multilline comment
"""
如果你在一个类中,你应该正确地标记它

例如:

class weather2():
   """
   def getStatus_code(self, url):
       world.url = url
       result = requests.get(url)
       return result.status_code
   """

从公认的答案来看


可以使用三重引号字符串。当它们不是docstring(类/函数/模块中的第一件事)时,它们将被忽略

'''
This is a multiline
comment.
'''
这根本不是事实。与注释不同,三引号字符串仍然被解析,并且必须在语法上有效,无论它们出现在源代码中的什么位置

如果您尝试运行此代码

def parse_token(token):
    """
    This function parses a token.
    TODO: write a decent docstring :-)
    """

    if token == '\\and':
        do_something()

    elif token == '\\or':
        do_something_else()

    elif token == '\\xor':
        '''
        Note that we still need to provide support for the deprecated
        token \xor. Hopefully we can drop support in libfoo 2.0.
        '''
        do_a_different_thing()

    else:
        raise ValueError
你要么

ValueError: invalid \x escape
…在Python2.x或

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 79-80: truncated \xXX escape
…在Python3.x上

唯一的方法来做多行注释,这是忽略了解析器是

elif token == '\\xor':
    # Note that we still need to provide support for the deprecated
    # token \xor. Hopefully we can drop support in libfoo 2.0.
    do_a_different_thing()
在Python 2.7.13上:

单一:

"A sample single line comment "
多行:

"""
A sample
multiline comment
on PyCharm
"""
好的,你可以试试这个(当运行引号时,第一个问题的输入应该用
”引号括起来):

之间包含的任何内容都将被注释


如果您正在查找单行注释,那么如果您在其中添加注释,则是
#

"""
long comment here
"""
在脚本编写过程中,Python/linters不会意识到这一点。折叠会搞砸,因为上面的注释不是标准建议的一部分。最好使用

# Long comment
# here.
如果您使用,您可以像这样的插件,通过按
Vjgcc
自动注释掉长行注释。其中
Vj
选择两行代码,然后
gcc
注释掉它们

如果你不想使用上面的插件,你可以使用搜索和替换

:.,.+1s/^/# /g

这将用
#
替换当前行和下一行的第一个字符。不幸的是,字符串化并不总是被用作注释!因此,在每行前面加上
更安全

以下是一个例子:

test1 = [1, 2, 3, 4,]       # test1 contains 4 integers

test2 = [1, 2, '''3, 4,'''] # test2 contains 2 integers **and the string** '3, 4,'

Python中的多行注释:

对我来说,“和”都起作用了

例如:

a = 10
b = 20
c = a+b
'''
print ('hello')
'''
print ('Addition is: ', a+b)
a = 10
b = 20
c = a+b
"""
print('hello')
"""
print('Addition is: ', a+b)
例如:

a = 10
b = 20
c = a+b
'''
print ('hello')
'''
print ('Addition is: ', a+b)
a = 10
b = 20
c = a+b
"""
print('hello')
"""
print('Addition is: ', a+b)

没有多行注释这样的功能
#
是注释一行代码的唯一方法。 你们中的许多人回答了“一条评论”,将此作为他们的解决方案

它似乎可以工作,但Python中的内部
'
将包含的行作为常规字符串,解释器不会忽略这些行,就像使用
注释一样


Python中实际上不存在多行注释。下面的示例由一个未赋值字符串组成,Python会验证该字符串是否存在语法错误

一些文本编辑器,比如,为我们提供了注释一段代码或文字的快捷方式

def foo():
    "This is a doc string."
    # A single line comment
    """
       This
       is a multiline
       comment/String
    """
    """
    print "This is a sample foo function"
    print "This function has no arguments"
    """
    return True

此外,Ctrl+K是记事本++中阻止注释的快捷方式。它在所选内容下的每一行前面添加一个
#
。Ctrl+Shift+K表示块取消注释。

Python中的内联注释以哈希字符开头

hello = "Hello!" # This is an inline comment
print(hello)
dial = "Dial #100 to make an emergency call."
print(dial)
你好!

请注意,字符串文字中的哈希字符只是一个哈希字符

hello = "Hello!" # This is an inline comment
print(hello)
dial = "Dial #100 to make an emergency call."
print(dial)
拨100打紧急电话

散列字符也可用于单行或多行注释

hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)
'''
I don't care the parameters and
docstrings here.
'''
你好

世界

用三个双引号将文本括起来以支持docstring

def say_hello(name):
    """
    This is docstring comment and
    it's support multi line.
    :param name it's your name
    :type name str
    """
    return "Hello " + name + '!'


print(say_hello("John"))
你好,约翰

用三个单引号将文本括起来,用于块注释

hello = "Hello"
world = "World"
# First print hello
# And print world
print(hello)
print(world)
'''
I don't care the parameters and
docstrings here.
'''

选择要注释的行,然后使用Ctrl+?在编辑器中注释或取消注释Python代码

对于单行,可以使用Shift+#。

使用PyCharm IDE

您可以
评论def my_function(arg1):
    """
    Summary line.
    Extended description of function.
    Parameters:
    arg1 (int): Description of arg1
    Returns:
    int: Description of return value
    """
    return arg1

print my_function.__doc__
print("Hello World")  # printing
# single line comment
"""
This is a comment
written in
more than just one line
"""