有没有一种机制可以注释掉大块的Python代码?
现在,我能看到注释掉代码的唯一方法是用a开始每一行#,或用三引号括起代码:""".
这些问题是#在每行之前插入很麻烦,"""并使我想用作注释的字符串显示在生成的文档中.
阅读完所有评论后,答案似乎是"不".
我正在使用Notepad ++作为编辑器来编写Python程序.这可能听起来很愚蠢,但我在编辑器中环顾四周,找不到任何方法(不是手动方式,而是在Emacs中的方法)在我的代码中做一个块注释.
由于Notepad ++支持如此多的语言设置,我很想找到一种方法来阻止注释代码.
有没有办法在Python中提供多行注释?
就像C/C++一样:/*comment*/.或者它是否必须在每行前面标记为"#"?
我在 Google 上搜索过,但找不到任何有关用注释突出显示代码部分的推荐方法的信息。
我在想这样的事情:
###########################################################
# This section is clearly labeled using comments so you can
# instantly identify its purpose within this file.
###########################################################
Run Code Online (Sandbox Code Playgroud)
php例如,我经常看到这种事情。
python中有这种事情的约定吗?如果不是,是否有充分的理由避免它?
如果这通常是使用多行注释完成的,有人可以提供一个示例来说明如何以这种“标题”样式完成此操作吗?
编辑:我知道多行注释。但通常我将它们与单个函数而不是一段代码相关联。更具体地说,我正在为单个文件中的小游戏考虑这样的事情:
###################################################
# CONFIGURATION
##################################################
configuration code...
###################################################
# HELPER FUNCTIONS
#################################################
helper functions.....
###################################################
# GRAPHICS HANDLING
#######################################################
graphics handling....
Run Code Online (Sandbox Code Playgroud) 使用空闲 Python 3.4.3。这是一个脚本,它给用户一个小测验,然后计算有多少是正确的。在我的脚本运行之前,我在评论中遇到了无效的语法错误。这是评论周围的整个代码。具体评论在下面一行score = decimal.Decimal(score):
score = amountright/7*100 """this takes the amount of questions the user got right, divides it by 7 (the total number of questions), then multiplies it by 100 to get a percentage correct and stores it in the variable score"""
import decimal """this will import a function to round off the final percentage to a whole number instead of an unnecessarily long decimal"""
score = decimal.Decimal(score)
"""this redefines the score variable as some sort …Run Code Online (Sandbox Code Playgroud) 我偶尔使用Python几个月,我知道我们可以使用#和""或""来评论.但是当我想评论一些字典的项目时,用评论词('''),我失败了.
testItems = {
'TestOne':
{
"NameId":101
"Score":99
},
'''
'TestTwo':
{
"NameId":101
"Score":99
}
'''
}
Run Code Online (Sandbox Code Playgroud)
然后我得到SyntaxError: invalid syntax指向最后'''位置的错误.
我也知道python语言有一些缩进规则.但我尝试了这么多缩进的可能性,仍然失败了.
I was working with simple if-else statements in Python when a syntax error came up with the following code.
"""
A multi-line comment in Python
"""
if a==b:
print "Hello World!"
"""
Another multi-line comment in Python
"""
else:
print "Good Morning!"
Run Code Online (Sandbox Code Playgroud)
This code gives a syntax error at the "else" keyword.
The following code however does not:
"""
A multi-line comment in Python
"""
if a==b:
print "Hello World!"
#One single line comment
#Another single line comment
else:
print …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 2.7并编写了以下内容:
def算术(A):
x = 1
“”
一些评论在这里
“”
如果x = 1:
x = 1
elif x = 2:
x = 2
返回0
但是它有缩进的问题:
如果x = 1:
^
IndentationError:意外缩进
那么如何在函数中写注释呢?
在python中你可以这样说:
x = """""" # x = ''
Run Code Online (Sandbox Code Playgroud)
Python词法分析器是否将此视为两个三重引号而内部没有任何内容?即沿着x = """ """(没有空间)的路线?
这是我的直接想法.但是,这在python中是可能的:
>>> "4" "5"
'45'
>>> # and
>>> "4""5"
'45'
Run Code Online (Sandbox Code Playgroud)
所以我可以看到,x = """"""也可能是沿着x = "" "" ""(没有空格)的线条.我只是想知道,是""""""作为两个三重引号还是三对正常引号?或完全不同的东西?谢谢!
编辑:显然,作为Python中的程序员并不重要.但是,Python解释器肯定必须选择其中之一,我想知道哪个.
我有一个 Django 项目,在某些地方我们有缩进的多行注释,如下所示:
field = models.IntegerField(default=0, null=True) # 0-initial_email_sent
# 1-second_email_sent
# 2-third_email_sent
Run Code Online (Sandbox Code Playgroud)
这显然违反了 PEP,但在我看来,有助于提高可读性。当然,我可以这样评论:
# 0-initial_email_sent
# 1-second_email_sent
# 2-third_email_sent
field = models.IntegerField(default=0, null=True)
Run Code Online (Sandbox Code Playgroud)
,但我更喜欢第一个。
有没有办法在不违反 PEP 8 的情况下缩进注释?
mo 在 f 字符串中包含注释会很有用。例如,拿这个代码:
f"""
<a
href="{ escape(url) }"
target="_blank" { # users expect link to open in new tab }
>bla</a>
"""
Run Code Online (Sandbox Code Playgroud)
如果此代码等效于:
f"""
<a
href="{ escape(url) }"
target="_blank"
>bla</a>
"""
Run Code Online (Sandbox Code Playgroud)
您可以在大括号之间包含完整的 Python 表达式,但似乎不能包含注释。我对么?有没有办法做到这一点?
我到处都告诉我,多行注释可以创建为:
'''
This is a multiline
comment.
'''
Run Code Online (Sandbox Code Playgroud)
(例如,参见此答案,以及更多)。
但是,当我在python或ipython 终端中执行此操作时,我看到打印了多行“注释”。
>>> '''
... This is a multiline
... comment.
... '''
'\nThis is a multiline\ncomment.\n'
>>>
Run Code Online (Sandbox Code Playgroud)
这不是我期望的行为。我被认为是上面的代码等效于使用哈希表示注释:
>>> # This is also a multiline
... # comment.
...
>>>
Run Code Online (Sandbox Code Playgroud)
如我所料,它根本不会打印任何内容。
那么,这里有什么呢?到处都在告诉我可以使用'''or """语法创建多行注释。但是,当我直接在终端中工作时,我没有观察到这种假定的行为。
我的第一个示例中的行为是否因为我的注释被解释为文档字符串并因此被打印出来?
python ×11
comments ×5
docstring ×1
f-string ×1
if-statement ×1
notepad++ ×1
pep8 ×1
python-idle ×1
syntax ×1