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

孤者浪人 提交于 2019-12-08 19:39:27

问题


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])

回答1:


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.




回答2:


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



回答3:


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



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/33147599/python-pep-8-e122-continuation-line-missing-indentation-or-outdented

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!