如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
如何做多行注释?大多数语言都有块注释符号,比如:
/*
*/
当前回答
在其他答案中,我发现最简单的方法是使用IDE注释函数,该函数使用Python注释支持#。
我正在使用Anaconda Spyder,它有:
Ctrl + 1 -注释/取消注释 Ctrl + 4 -注释代码块 Ctrl + 5 -取消注释代码块
它可以用#注释/取消注释一行/多行代码。
我觉得这是最简单的。
例如,一个块注释:
# =============================================================================
# Sample Commented code in spyder
# Hello, World!
# =============================================================================
其他回答
Visual Studio Code通用官方多行注释切换。类似于Xcode的快捷方式。
macOS:选择code-block,然后选择⌘+/
Windows:选择code-block,然后按Ctrl+/
好吧,你可以试试这个(当运行引号时,第一个问题的输入应该用'引号括起来):
"""
print("What's your name? ")
myName = input()
print("It's nice to meet you " + myName)
print("Number of characters is ")
print(len(myName))
age = input("What's your age? ")
print("You will be " + str(int(age)+1) + " next year.")
"""
a = input()
print(a)
print(a*5)
"""之间的任何内容都将被注释。
如果你正在寻找单行注释,那么它是#。
要注释掉Python中的多行代码,只需在每一行上使用# single-line注释:
# This is comment 1
# This is comment 2
# This is comment 3
要在Python中编写“正确的”多行注释,就是使用带有“”语法的多行字符串 Python有文档字符串(或文档字符串)特性。它为程序员提供了一种简单的方法,可以为每个Python模块、函数、类和方法添加快速注释。
'''
This is
multiline
comment
'''
另外,提到您可以通过这样的类对象访问docstring
myobj.__doc__
在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
"""
不幸的是,字符串化不能总是用于注释!因此,坚持在每行前加上#的标准更安全。
这里有一个例子:
test1 = [1, 2, 3, 4,] # test1 contains 4 integers
test2 = [1, 2, '''3, 4,'''] # test2 contains 2 integers **and the string** '3, 4,'