user132290
user132290

Reputation: 189

Python, PEP-8, E122 continuation line missing indentation or outdented

I get this error but however I choose to indent it, I still get it, do you know why?

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}"\
    .format(argmaxcomp[0])

Upvotes: 13

Views: 36300

Answers (5)

Malik A. Rumi
Malik A. Rumi

Reputation: 2015

Just had a similar issue and resolved it. I think the problem with the OP's code is that there may be whitespace between the continuation lines. There should be nothing there but the \n.

Upvotes: 0

st0ne
st0ne

Reputation: 4265

The problem in this case is that there is no indentation at all and obviously the error occurs in the last line. In case parentheses are not an option just add indentation as below:

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}" \
        .format(argmaxcomp[0])

Any amount of spaces works but I don't know what is preferred.

Upvotes: 1

MSeifert
MSeifert

Reputation: 152587

There is one section in the PEP8 that reads:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Backslashes may still be appropriate at times. For example, long, multiple with -statements cannot use implicit continuation, so backslashes are acceptable

This means (even though this has nothing to do with PEP8-E122) that you should wrap it in paranthesis instead of using the backslash and then the implicit line continuation (indentation) is the opening bracket:

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is: {0}"
          .format(argmaxcomp[0]))
#         ^--------- The bracket opens here

There are only 2 exceptions mentioned where the backslash is acceptable because paranthesis are impossible (because they have another meaning in these contexts):

  • multi-with
  • asserts

However if you really want that backslash (only possible with python2) it should have the same indentation as the first expression:

if len(argmaxcomp) == 1:
    print "The complex with the greatest mean abundance is: {0}" \
          .format(argmaxcomp[0])
#         ^--------- The first expression starts here

Upvotes: 0

Andy Hayden
Andy Hayden

Reputation: 375375

Generally pep8 suggests you prefer parenthesis over continuation lines.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

That is:

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is: {0}"
          .format(argmaxcomp[0]))

Another option, is to use python 3's print:

from __future__ import print_function

if len(argmaxcomp) == 1:
    print("The complex with the greatest mean abundance is:", argmaxcomp[0])

Note: print_function may break/require updating the rest of the code... anywhere you've used print.

Upvotes: 11

Muthuvel
Muthuvel

Reputation: 526

I did't get the above error, But i have tried below types, Please post with error, so that we can check

In [6]: argmaxcomp = [100]

In [7]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'\
   ...:     .format(argmaxcomp[0])
   ...:     
val: 100

In [8]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'.format(argmaxcomp[0])
   ...:     
val: 100

In [9]: if len(argmaxcomp) == 1:
   ...:     print 'val: {0}'.format(
   ...:     argmaxcomp[0])
   ...:     
val: 100

Upvotes: 0

Related Questions