【问题标题】:PEP8: continuation line over-indented for visual indentPEP8:为视觉缩进过度缩进的延续线
【发布时间】:2014-03-23 17:12:05
【问题描述】:

我有这行代码,在测试 pep8 错误时,我得到: 线太长。因此,为了尝试解决此问题,我使用了 slash('\') 但随后我将延续行过度缩进以进行视觉缩进。我该怎么做才能解决这个问题?

我尝试过的事情:

if first_index < 0 or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 \ 
   or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 or \
   second_index > self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:
     raise ValueError

continuation line over-indented for visual indent

【问题讨论】:

    标签: python


    【解决方案1】:

    行扩展反斜杠的问题是尾随空格会破坏您的代码。这是一种流行的修复方法,并且符合 PEP8:

    if (first_index < 0 or
        second_index > self._number_of_plates - 1):
    

    【讨论】:

    • 谢谢大卫,但是现在我得到'续行不能与下一个逻辑行区分开来'与那行代码。编辑:固定!
    • 对于未来的读者 - 这意味着下一行具有相同的缩进,因此在这种情况下,上面代码中的第二个可以向右移动 1 个标签。
    • 缩进第二行会严重影响可读性恕我直言。我在这里的技巧是删除括号并用反斜杠标记行继续。我发现的唯一问题是,当尝试在单个“with”中使用多个“open()”上下文管理器时,它不能解决任何问题。添加括号会导致完整的语法错误,这让我有点好奇这与标准元组的解析方式有何不同。
    【解决方案2】:

    续行的缩进比视觉缩进的距离更远。

    反模式 在本例中,字符串“World”的缩进比应有的缩进了两个空格。

    print("Python", ("Hello",
                       "World"))
    

    最佳实践

    print("Python", ("Hello",
                     "World"))
    

    参考:https://www.flake8rules.com/rules/E127.html

    【讨论】:

    • 来自 Flake8 规则的 print() 示例非常幼稚。来自现实世界的具体代码示例会更合适。
    【解决方案3】:

    对于太长的行(例如 > 79 个字符),您可以使用括号对条件进行分组:

    if (first_index < 0 
            or second_index > self._number_of_plates - 1
            or condition2
            and candition3):
        raise ValueError
    

    请注意,任何布尔条件(orand)都应位于条件之前的行首。

    在您的情况下,由于 if (...) 构造有一个特殊规则:

    当 if 语句的条件部分足够长时需要 它被写成多行,值得注意的是 两个字符关键字(即 if)的组合,加上一个空格, 加上一个左括号为 多行条件的后续行。这可以产生一个 与嵌套在 if 语句,它也自然会缩进 4 个空格。这 PEP 没有明确表示如何(或是否)进一步视觉化 将此类条件行与 if 语句。在这种情况下可接受的选项包括,但 不限于:

    # No extra indentation.
    if (this_is_one_thing and
        that_is_another_thing):
        do_something()
    
    # Add a comment, which will provide some distinction in editors
    # supporting syntax highlighting.
    if (this_is_one_thing and
        that_is_another_thing):
        # Since both conditions are true, we can frobnicate.
        do_something()
    
    # Add some extra indentation on the conditional continuation line.
    if (this_is_one_thing
            and that_is_another_thing):
        do_something()
    

    (另见是否在二进制之前或之后中断的讨论 下面的运算符。)

    来源:PEP 8 Style Guide

    【讨论】:

      【解决方案4】:

      这就是流行的black 格式化程序所做的:

      if (
          first_index < 0
          or second_index > self._number_of_plates - 1
      ):
          raise ValueError
      

      这对我来说很好。然而,重构代码可能是一个好主意,让代码一开始就没有 7 级缩进!

      【讨论】:

        猜你喜欢
        • 2014-03-02
        • 2015-11-25
        • 2020-01-14
        • 2013-03-04
        • 2015-05-30
        • 2014-05-26
        • 2017-05-24
        • 2016-07-11
        • 2019-12-12
        相关资源
        最近更新 更多