如何做多行注释?大多数语言都有块注释符号,比如:

/*

*/

当前回答

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

一些文本编辑器,如notepad++,为我们提供了注释掉一段书面代码或单词的快捷方式。

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是notepad++中阻止注释的快捷键。它在选定的每一行前面添加一个#。Ctrl + Shift + K表示取消注释。

其他回答

AFAIK, Python没有块注释。对于注释单个行,可以使用#字符。

如果你正在使用notepad++,有一个块注释的快捷方式。我相信其他的像gVim和Emacs也有类似的特性。

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

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

这可以在Vim文本编辑器中完成。

转到评论区第一行的开头。

按“Ctrl+V”进入可视模式。

使用方向键选择要注释的所有行。

按下Shift +我。

按#(或Shift+3)。

按Esc。

在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
   """

我阅读了各种方法的缺点,我想出了这个方法,试图检查所有的盒子:

block_comment_style = '#[]#'
'''#[
class ExampleEventSource():
    def __init__(self):
        # create the event object inside raising class
        self.on_thing_happening = Event()

    def doing_something(self):
        # raise the event inside the raising class
        self.on_thing_happening()        
        
        
class ExampleEventHandlingClass():
    def __init__(self):
        self.event_generating_thing = ExampleEventSource()
        # add event handler in consuming class
        event_generating_thing.on_thing_happening += my_event_handler
        
    def my_event_handler(self):
        print('handle the event')
]#'''


class Event():
 
    def __init__(self):
        self.__eventhandlers = []
 
    def __iadd__(self, handler):
        self.__eventhandlers.append(handler)
        return self
 
    def __isub__(self, handler):
        self.__eventhandlers.remove(handler)
        return self
 
    def __call__(self, *args, **keywargs):
        for eventhandler in self.__eventhandlers:
            eventhandler(*args, **keywargs)

Pros

对于其他程序员来说,这显然是一个注释。这是自描述的。 它编译 它不会在help()中显示为doc注释 如果需要,它可以位于模块的顶部 它可以通过宏自动实现。 [注释]不是代码的一部分。它不会在pyc中结束。(除了支持优点#1和#4的一行代码) 如果在Python中添加了多行注释语法,则可以使用find和replace修复代码文件。简单地使用“”并没有这个优势。

Cons

很难记住。打字量很大。这个骗局可以用宏来消除。 这可能会让新手感到困惑,认为这是惟一的块注释方法。这可能是专业的,只是取决于你的角度。它可能会让新手认为这行代码神奇地连接到注释“working”。 它不会作为注释着色。但话又说回来,没有一个答案能真正解决OP问题的精神。 这不是官方的方式,所以Pylint可能会抱怨。我不知道。也许;也许不是。

这里是一个VS Code宏的尝试,尽管我还没有测试它:

{
    "key": "ctrl+shift+/",
    "command": "editor.action.insertSnippet",
    "when": "editorHasSelection"
    "args": {
        "snippet": "block_comment_style = '#[]#'\n'''#[{TM_SELECTED_TEXT}]#'''"
    }
}