简体   繁体   English

Python 3.x多行注释会引发语法错误

[英]Python 3.x multi line comment throws syntax error

I'm working on a Python project and as of now, my code has over 400+ lines. 我正在研究一个Python项目,到目前为止,我的代码有超过400行。 At one point, I had to write a multi-line comment about a small bug which needs a work around, and the interpreter decided to throw a syntax error. 有一次,我不得不写一个关于需要解决的小bug的多行注释,并且解释器决定抛出语法错误。

According to the interpreter, the syntax error is occuring at elif . 根据解释器,语法错误发生在elif I re-checked my indentation, converted tabs to spaces etc. Nothing seems to work. 我重新检查了我的缩进,将标签转换为空格等。似乎没什么用。

                    if some_condition_1 == True:
                       do_something()

                    """
                    Sub stage (b):
                    Refer documentation [1.7A] for ...
                    ....
                    ....
                    ....
                    """
                    elif condition_1 == True:
                        if condition_2 == False:
                            list.append(item)

However, if I remove the multi-line comment, the code executes fine. 但是,如果我删除多行注释,代码执行正常。

Any idea what's going wrong? 知道出了什么问题吗? Please note that the code sample I've show above, is at very top of the file, and there's no chance for anything to go wrong elsewhere. 请注意,我上面显示的代码示例位于文件的最顶层 ,并且在其他地方没有任何出错的机会。

This is an indentation error. 这是缩进错误。 Your "multi-line comment" (really multi-line string) must be indented under the if block just like anything else. 您的“多行注释”(实际上是多行字符串)必须在if块下缩进,就像其他任何内容一样。

""" These kinds of things """ are not really comments in Python. """ These kinds of things """在Python中并不是真正的评论。 You're just creating a string and then throwing the value away (not storing it anywhere). 你只是创建一个字符串,然后抛弃价值(不存储在任何地方)。 Since Python doesn't have true multi-line comments, many people use them this way. 由于Python没有真正的多行注释,许多人以这种方式使用它们。 However, since they are not true comments (they aren't ignored by the interpreter), they must obey all normal syntax rules, including indentation rules. 但是,由于它们不是真正的注释(解释器不会忽略它们),因此它们必须遵守所有正常的语法规则,包括缩进规则。

(Do note that when I say "creating a string" I'm speaking loosely. CPython, at least, has an optimization not to create an object here.) (请注意,当我说“创建一个字符串”时,我说的是松散的。至少CPython有一个优化,不在这里创建一个对象。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM