如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
如果在带有代码的一行中编写注释,则必须在#符号之前留下2个空格,在#符号之前留下1个空格
print("Hello World") # printing
如果在新行上写注释,必须写注释,在#符号中留下1个空格kn
# single line comment
要编写超过一行的注释,需要使用3个引号
"""
This is a comment
written in
more than just one line
"""
其他回答
在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.
'''
(确保适当缩进前导" '以避免出现IndentationError。)
Guido van Rossum (Python的创造者)在推特上发布了这条“专业提示”。
然而,Python的风格指南PEP8倾向于使用连续的单行注释,像这样:
# This is a multiline
# comment.
...这也是你在很多项目中会发现的。文本编辑器通常有一个快捷方式来轻松做到这一点。
我阅读了各种方法的缺点,我想出了这个方法,试图检查所有的盒子:
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}]#'''"
}
}
Python中的内嵌注释以哈希字符开始。
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)
你好 世界
用三双引号将文本括起来以支持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"))
你好约翰!
在文本中加上三个单引号作为块注释。
'''
I don't care the parameters and
docstrings here.
'''
如果在带有代码的一行中编写注释,则必须在#符号之前留下2个空格,在#符号之前留下1个空格
print("Hello World") # printing
如果在新行上写注释,必须写注释,在#符号中留下1个空格kn
# single line comment
要编写超过一行的注释,需要使用3个引号
"""
This is a comment
written in
more than just one line
"""