What is the correct way to format long lines of code in python?
Question: I've got some lines of code that return and assign multiple values from a
function, like below. This of course makes it one very long line. What's the
correct way to style/format a line of code like this? I've included a couple
of possibilities that I've come up with.

    
    
    some_variable_1, \
        some_variable_2, \ 
        some_variable_3, \
        some_variable_4, \
        some_variable_5 = sort_of_long_function_name(parameter_1, parameter_2,
        parameter_3, parameter_4)
    
    some_variable_1, some_variable_2, some_variable_3, some_variable_4, \
        some_variable_5 = sort_of_long_function_name(parameter_1, parameter_2,
        parameter_3, parameter_4)
    

Edit: Thanks all for the answers. I can't upvote because I'm a noob heh(no
rep), so I just wanted to say that I appreciate it. I've read the style guide
before and typically I do avoid long lines like this. In this case, I feel I
need this function to calculate and return multiple stats. I think I will make
some of my naming a bit more concise and look into returning an object or
named tuple. Thanks!


Answer: You want to _avoid_ getting into that situation. If you have a tuple
assignment for that many names and you cannot even fit that on a line in 80
characters, it is time for a refactor.

For example, you could have your function return a [`namedtuple`
class](https://docs.python.org/2/library/collections.html#collections.namedtuple)
instead:

    
    
    from collections import namedtuple
    
    Result = namedtuple('Result', 'foo bar spam ham eggs')
    
    def sort_of_long_function_name(arg1, arg2, arg3, arg4):
        return Result(foo_result, bar_result, spam_result, ham_result, eggs_result)
    
    result = sort_of_long_function_name(param1, param2, param3, param4, param5)
    do_something_with(result.foo)
    do_something_else_with(result.bar)
    # etc.
    

Now instead of 5 variables, you have one with helpful named attributes.

This is what the parsing functions in the [`urlparse`
module](https://docs.python.org/2/library/urlparse.html) do, with the
`namedtuple` classes even having some additional methods to re-create the
parsed URL.

