I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings,
e = 'a' + 'b' + 'c' + 'd'
and have it like this:
e = 'a' + 'b' +
'c' + 'd'
I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax? For example, adding a bunch of strings,
and have it like this:
|
||||
|
What is the line? You can just have arguments on the next line without any problems:
Otherwise you can do something like this:
Check the style guide for more information. From your example line:
Or:
Note that the style guide says that using the implicit continuation with parentheses is preferred, but in this particular case just adding parentheses around your expression is probably the wrong way to go. |
|||||||||||||||||||||
|
From Style Guide for Python Code:
EDIT: PEP8 now recommends the opposite convention (for breaking at binary operations) used by Mathematicians and their publishers to improve readability. Donald Knuth's style of breaking before a binary operator aligns operators vertically, thus reducing the eye's workload when determining which items are added and subtracted. From PEP8: Should a line break before or after a binary operator?:
[3]: Donald Knuth's The TeXBook, pages 195 and 196 |
|||||||||||||||||||||
|
The danger in using a backslash to end a line is that if whitespace is added after the backslash (which, of course, is very hard to see), the backslash is no longer doing what you thought it was. See Python Idioms and Anti-Idioms (for Python 2 or Python 3) for more. |
|||||
|
You can break lines in between parenthesises and braces. Additionally, you can append the backslash character
|
|||
|
Put a
or
|
|||
|
|
|||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?