简体   繁体   English

在Python中以反斜线继续行是否危险?

[英]Is line continuation with backslash dangerous in Python?

I understand that current best practice for line continuation is to use implied continuation inside parenthesis. 我知道,行连续的当前最佳实践是在括号内使用隐式连续。 For example: 例如:

a = (1 + 2
     + 3 + 4)

From PEP8 ( https://www.python.org/dev/peps/pep-0008/ ): 从PEP8( https://www.python.org/dev/peps/pep-0008/ ):

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. 包装长行的首选方法是在括号,方括号和花括号内使用Python的隐含行连续性。 Long lines can be broken over multiple lines by wrapping expressions in parentheses. 通过将表达式包装在括号中,可以将长行分成多行。 These should be used in preference to using a backslash for line continuation. 应优先使用这些,而不是使用反斜杠进行行连续。

I intend to follow this convention going forward, however, my question regards how worried I should be about bugs in existing code that continues lines with a backslash: 我打算遵循这个约定,但是,我的问题是我应该如何担心现有代码中以反斜杠继续的错误:

a = 1 + 2 \
    + 3 + 4

The python docs ( https://docs.python.org/2.7/howto/doanddont.html#using-backslash-to-continue-statements ) warn that a stray space at the end of a line after the backslash can make the code "subtly wrong," however, as pointed out in this question ( How can using Python backslash line continuation be subtly wrong? ), the example given simply results in a SyntaxError being raised which is not a subtle problem as it is easily identified. python docs( https://docs.python.org/2.7/howto/doanddont.html#using-backslash-to-continue-statements )警告说,反斜杠后一行的末尾有一个空格可以使代码但是,如该问题中所指出的那样(“非常错误”)( 使用Python反斜杠行的连续性怎么可能是非常错误的? ),给出的示例仅导致引发SyntaxError,这不是一个微妙的问题,因为它很容易识别。 My question, then, is do there exist cases where continuing a line with a backslash causes something worse than a parsing error? 那么,我的问题是,是否存在用反斜杠继续行导致比解析错误更糟的情况? I am interested in examples where a mistake in backslash continuations yields a runtime exception or, worse, code that runs silently but with unintended behavior. 我对一些示例感兴趣,这些示例中反斜杠连续的错误会导致运行时异常,或者更糟的是,这些代码以静默方式运行但具有意外行为。

One case I can think of is when the programmer forgets a \\ . 我能想到的一种情况是程序员忘记了\\ This is especially likely when someone comes and adds values at a later date. 当某人以后来添加值时,这种情况尤其可能发生。 Though this example is still somewhat contrived because the continuation lines have to have the same indentation level (otherwise it would fail with an IndentationError). 尽管此示例还是有些人为的,因为连续行必须具有相同的缩进级别(否则,它将因IndentationError失败)。

Example: 例:

a = 1 \
+ 2 \
+ 3
assert a == 6

Later, someone adds a line: 后来,有人添加了一行:

a = 1 \
+ 2 \
+ 3    # Whoops, forgot to add \ !
+ 4
assert a == 10  # Nope

"Subtly wrong" is subjective; “严重错误”是主观的; each has her own tolerance of subtlety. 每个人都有自己的微妙包容。

One often-cited example of possibly harmful line continuation, regardless of \\ -separated or implicit, is the continuation of strings in a container structure: 一个经常被引用的可能有害行连续的示例,无论\\分隔还是隐式,都是容器结构中字符串的连续:

available_resources = [
    "color monitor",
    "big disk",
    "Cray"
    "on-line drawing routines",
    "mouse",
    "keyboard",
    "power cables",
]

Do the available resources include a Cray supercomputer, or Crayon-line drawing routines ? 可用资源是否包括Cray超级计算机或Crayon-line drawing routines (This example is from the book "Expert C Programming", adapted for Python here). (此示例摘自《 Expert C Programming》一书,此处适用于Python)。

This code block isn't ambiguous, but its visual appearance created by continuation + indentation can be deceptive, and may cause human error in understanding. 该代码块不是模棱两可的,但由连续+缩进创建的视觉外观可能具有欺骗性,并可能导致人为理解错误。

I think like @0x5453, if you wrongly add backslash to your code. 我认为就像@ 0x5453一样,如果您错误地在代码中添加了反斜杠。
This backslash cause the comment to be concat with a. 此反斜杠导致注释与a连在一起。

a = "Some text. " \
"""Here are some multiline comments
that will be added to a""" 

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

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