具有单个条件的多行 If 语句

Multiline If statement with a single conditional

假设我有两个变量

self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication 

self.SuperLongSpecificCorperateVariableNameIcantChangeControl 

我需要比较它们。

问题是,当我将它们都放在 if 语句中时,它超过了样式检查器的行长度。

if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication != self.SuperLongSpecificCorperateVariableNameIcantChangeControl):

解决这个问题的方法是将其分成两行。

if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication \
    != self.SuperLongSpecificCorperateVariableNameIcantChangeControl):

我的同事对 PEP 8 是否让你在条件之间进行拆分,或者你是否可以拆分条件本身存在分歧。理想情况下,我们会获得更改变量名称的批准,但与此同时,PEP 8 说在这种情况下我们应该做什么?

根据PEP8

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.

还有一个关于 multiline if-statements 的讨论没有明确的立场,但建议在条件的续行中添加缩进级别。

最后,Should a Line Break Before or After a Binary Operator? 建议在 运算符之前中断 (就像您所做的那样)。所以你可以写:

if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication
        != self.SuperLongSpecificCorperateVariableNameIcantChangeControl):
    pass

但是 Python 中的变量只是名称 - 一种引用对象的方式。名称可以更改。可以避免换行:

communication = self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication 
control = self.SuperLongSpecificCorperateVariableNameIcantChangeControl 

if communication != control:
    pass

首先,PEP 8 说你可以在 Maximum Line Length:

下拆分长行

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.

事实上,由于括号的缘故,您示例中的反斜杠是不需要的。


PEP 8 说你可以在 multiline if-statements 下拆分一个条件,尽管该部分的主要重点是如何将它与下面的块区分开来。

When the conditional part of an if-statement is long enough to require that it be written across multiple lines, it's worth noting that the combination of a two character keyword (i.e. if), plus a single space, plus an opening parenthesis creates a natural 4-space indent for the subsequent lines of the multiline conditional. This can produce a visual conflict with the indented suite of code nested inside the if-statement, which would also naturally be indented to 4 spaces. This PEP takes no explicit position on how (or whether) to further visually distinguish such conditional lines from the nested suite inside the if-statement. Acceptable options in this situation include, but are not limited to:

# 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()

就个人而言,我会选择最后一个选项以获得最大的可读性。所以这给了我们:

if (self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication
        != self.SuperLongSpecificCorperateVariableNameIcantChangeControl):
    do_something()

其他选项

您可以使用临时的“内部使用”名称来缩短行:

_Comm = self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication
_Control = self.SuperLongSpecificCorperateVariableNameIcantChangeControl
if _Comm != _Control:
    do_something()

这是假设上下文不在本地范围内。如果它实际上在本地范围内,则不需要“内部使用”。


您可以使用辅助函数在本地范围内为它们提供更短的名称。由于它们是属性,您可以传入它们的对象:

def _compare(instance):
    a = instance.SuperLongSpecificCorperateVariableNameIcantChangeCommunication
    b = instance.SuperLongSpecificCorperateVariableNameIcantChangeControl
    return a != b

if _compare(self):
    do_something()