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

2024-10-01 07:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有两个变量

self.SuperLongSpecificCorperateVariableNameIcantChangeCommunication 

self.SuperLongSpecificCorperateVariableNameIcantChangeControl 

我需要比较它们

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

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

解决这个问题的方法是把它分成两行

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

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


Tags: 方法selfif情况样式语句pep理想
2条回答

根据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

首先,PEP8说可以在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.

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


PEP8说可以在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()

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


可以使用helper函数在局部范围内为它们指定较短的名称。因为它们是属性,所以可以传入它们的对象:

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

if _compare(self):
    do_something()

相关问题 更多 >