gpt4 book ai didi

python - 如何根据PEP8拆分多个命令?

转载 作者:行者123 更新时间:2023-12-01 04:17:32 25 4
gpt4 key购买 nike

我有以下命令:

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 个字符限制的首选方法是什么?

最佳答案

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'))

关于python - 如何根据PEP8拆分多个命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34155552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com