正确格式化python multiline [for in]语句

时间:2014-08-10 12:38:43

标签: python coding-style pep8 pep

我应该如何在python中格式化中的长语句?

for param_one, param_two, param_three, param_four, param_five in get_params(some_stuff_here, and_another stuff):

我发现只能使用反斜杠制作 for 语句:

for param_one, param_two, param_three, param_four, param_five \
in get_params(some_stuff_here, and_another_stuff):

但我的linter有这个格式的问题,什么是 Pythonic 方式 这样的格式化语句?

2 个答案:

答案 0 :(得分:2)

all_params = get_params(some_stuff_here, and_another_stuff)
for param_one, param_two, param_three, param_four, param_five in all_params:
    pass

或者你可以在循环中移动目标列表:

for params in get_params(some_stuff_here, and_another_stuff):
    param_one, param_two, param_three, param_four, param_five = params
    pass

或两者结合。

答案 1 :(得分:1)

您可以利用括号内隐含的连线(如PEP-8中所建议的那样):

for (param_one, param_two, 
     param_three, param_four, 
     param_five) in get_params(some_stuff_here, 
                               and_another stuff):

(显然,您可以选择每条线的生成时间以及是否需要在每组括号中包含换行符。)