pythoncoding-stylepep8pep

proper formatting of python multiline [ for in ] statement


How should i format a long for in statement in python ?

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

I have found that i can brake a for in statement only with a backslash :

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

But my linter has issues with this formatting , what is a Pythonic way of formatting statements like this ?


Solution

  • 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
    

    Or you could move the target list inside the loop:

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

    Or combine both.