The Python Oracle

Is it possible to break a long line to multiple lines in Python?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC C Schuberts Piano Sonata No 13 D

--

Chapters
00:00 Question
00:24 Accepted answer (Score 747)
01:49 Answer 2 (Score 246)
02:49 Answer 3 (Score 46)
03:12 Answer 4 (Score 24)
03:25 Thank you

--

Full question
https://stackoverflow.com/questions/4172...

Question links:
[Python]: http://en.wikipedia.org/wiki/Python_%28p...

Accepted answer links:
[PEP 8 - Style Guide for Python Code]: http://www.python.org/dev/peps/pep-0008/

Answer 2 links:
[PEP8]: http://www.python.org/dev/peps/pep-0008/

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #linebreaks

#avk47



ACCEPTED ANSWER

Score 782


From PEP 8 - Style Guide for Python Code:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately.

Example of implicit line continuation:

a = (
    '1'
    + '2'
    + '3'
    - '4'
)


b = some_function(
    param1=foo(
        "a", "b", "c"
    ),
    param2=bar("d"),
)

On the topic of line breaks around a binary operator, it goes on to say:

For decades the recommended style was to break after binary operators. But this can hurt readability in two ways: the operators tend to get scattered across different columns on the screen, and each operator is moved away from its operand and onto the previous line.

In Python code, it is permissible to break before or after a binary operator, as long as the convention is consistent locally. For new code Knuth's style (line breaks before the operator) is suggested.

Example of explicit line continuation:

a = '1'   \
    + '2' \
    + '3' \
    - '4'



ANSWER 2

Score 254


There is more than one way to do it.

1). A long statement:

>>> def print_something():
         print 'This is a really long line,', \
               'but we can make it across multiple lines.'

2). Using parenthesis:

>>> def print_something():
        print ('Wow, this also works?',
               'I never knew!')

3). Using \ again:

>>> x = 10
>>> if x == 10 or x > 0 or \
       x < 100:
       print 'True'

Quoting PEP8:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it.




ANSWER 3

Score 52


If you want to assign a long string to variable, you can do it as below:

net_weights_pathname = (
    '/home/acgtyrant/BigDatas/'
    'model_configs/lenet_iter_10000.caffemodel')

Do not add any comma, or you will get a tuple which contains many strings!




ANSWER 4

Score 24


It works in Python too:

>>> 1+\
      2+\
3
6
>>> (1+
          2+
 3)
6