2

我应该如何在 python中格式化 long for in语句?

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

我发现我只能用反斜杠来制动for in语句:

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

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

4

2 回答 2

4

您可以利用括号内的隐式行连接(如 PEP-8 中所推荐的):

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

(显然,您可以选择每行的长度以及是否需要在每组括号中包含换行符。)

于 2014-08-10T16:09:23.313 回答
3
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

或者两者结合。

于 2014-08-10T12:43:55.243 回答