如何按照PEP8拆分多个命令?

How to split multiple commands according to PEP8?

我有以下命令:

DataFrame = df0.join(df1, how = 'outer').join(df2, how = 'outer').join(df3, how = 'outer').....

我知道我可以把它分开,像这样:

dataFrame = df0.join(df1, how = 'outer')
dataFrame = dataFrame.join(df2, how = 'outer')
dataFrame = dataFrame.join(df3, how = 'outer')
...

但我不确定 PEP8 对此怎么说,不超过 79 个字符限制的首选方法是什么?

您不应该为了满足 PEP 而创建虚拟变量。 但是,这里建议将命令拆分为多个

DataFrame = df0.join(df1, how = 'outer') \
               .join(df2, how = 'outer') \
               .join(df3, how = 'outer')

最后,在某些情况下,您不能使行短于 80 个字符!

在点后插入换行符。如果您用括号括起整个语句,您甚至不需要结尾的反斜杠。

dataFrame = (df0.
             join(df1, how='outer').
             join(df2, how='outer').
             join(df3, how='outer'))

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.

Backslashes may still be appropriate at times. For example, long, multiple with-statements cannot use implicit continuation, so backslashes are acceptable:

with open('/path/to/some/file/you/want/to/read') as file_1, \
     open('/path/to/some/file/being/written', 'w') as file_2:
    file_2.write(file_1.read())

Another such case is with assert statements.

Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.

https://www.python.org/dev/peps/pep-0008/

即举个例子:

dataFrame = (df0.join(df1, how='outer').
                 join(df2, how='outer').
                 join(df3, how='outer'))