在 Python 中使用反斜杠续行是否危险? [英] Is line continuation with backslash dangerous in Python?

查看:30
本文介绍了在 Python 中使用反斜杠续行是否危险?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解当前行延续的最佳做法是在括号内使用隐含的延续.例如:

I understand that current best practice for line continuation is to use implied continuation inside parenthesis. For example:

a = (1 + 2
     + 3 + 4)

来自 PEP8 (https://www.python.org/dev/peps/pep-0008/):

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

包装长行的首选方法是在括号、方括号和大括号内使用 Python 的隐含行继续符.通过将表达式括在括号中,可以将长行分成多行.这些应该优先于使用反斜杠作为行继续符.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. 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

python 文档(https://docs.python.org/2.7/howto/doanddont.html#using-backslash-to-continue-statements) 警告说,反斜杠后一行末尾的杂散空格会使代码微妙地错误," 但是,正如在这个问题中指出的那样(如何使用 Python 反斜杠行继续是微妙的错误?),给出的示例只会导致 SyntaxError 被引发,这不是一个微妙的问题,因为它很容易识别.那么,我的问题是,是否存在用反斜杠继续一行会导致比解析错误更糟糕的情况?我对反斜杠延续中的错误导致运行时异常,或者更糟糕的是,代码静默运行但具有意外行为的示例感兴趣.

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. 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.

推荐答案

微妙的错误"是主观的;每个人都有自己对微妙之处的容忍度.

"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",
]

可用资源是否包括 Cray 超级计算机或 蜡笔画线程序?(这个例子来自《Expert C Programming》一书,这里针对 Python 进行了改编).

Do the available resources include a Cray supercomputer, or Crayon-line drawing routines? (This example is from the book "Expert C Programming", adapted for Python here).

这个代码块没有歧义,但它由延续 + 缩进创建的视觉外观可能具有欺骗性,并可能导致人为理解错误.

This code block isn't ambiguous, but its visual appearance created by continuation + indentation can be deceptive, and may cause human error in understanding.

这篇关于在 Python 中使用反斜杠续行是否危险?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆