views:

203

answers:

4

Suppose you are calling a function, where there's clearly a need to break down the statement into few lines, for readability's sake. However there are at least two way to do it:

Would you do this:

return render(request, template,
              {
                'var1' : value1,
                'var2' : value2,
                'var3' : value3
               }
             )

Or would you rather do that:

return render \
(
    request, template,
    {
        'var1' : value1,
        'var2' : value2,
        'var3' : value3
    }
)

Or, please suggest your own formatting. Please also list reasons why would you use a particular formatting and what's wrong with the other one.

Thanks

+9  A: 

Python's official PEP-8 suggests the first one.

AlcariTheMad
+5  A: 

I'd probably do:

return render(
    request, 
    template,
    {
        'var1' : value1,
        'var2' : value2,
        'var3' : value3
    }
)

I would keep the bracket on the same line, so that searches for render( work. And because I find it clearer. But I'd put all the arguments on new lines.

Douglas Leeder
excellent point on searching for "render("
Art
+1 Interesting. I've never thought of this style.
jeffjose
+2  A: 

The second one looks like it escaped from a C[#+]* program. Backslash line continuation is ugly, prone to trouble with trailing space, and there's no excuse to use it when you've got () or [] to use.

John Machin
+1 never use backslash, there is always a better option (continuation inside (), [], {})
kaizer.se
from __future__ import braces
Beau Martínez
Perhaps you meant `from __future__ import braces`. `from SOhelp import backticks`
John Machin
+4  A: 

I would do:

vars = {
    'var1' : value1,
    'var2' : value2,
    'var3' : value3,
}
return render(request, template, vars)
Zach Hirsch
+1, most elegant of the approaches suggested.
Alex Martelli
+1 Just because you *can* do it all in one statement doesn't mean you *should*.
Jon-Eric