Reputation: 189
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
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
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
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):
with
assert
sHowever 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
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
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