Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP8: continuation line over-indented for visual indent

Tags:

python

I have this line of code which goes over the line and when testing for pep8 errors I get: line too long. So to try and fix this I used slash('\') but then I get continuation line over-indented for visual indent. What can I do to fix this?

enter image description here

Things I've tried:

if first_index < 0 or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 \ 
   or second_index > \
   self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index < 0 or \
   second_index > self._number_of_plates - 1:
    raise ValueError

continuation line over-indented for visual indent

if first_index \
   < 0 or second_index \
   > self._number_of_plates - 1:
     raise ValueError

continuation line over-indented for visual indent
like image 305
Sc4r Avatar asked Feb 21 '14 23:02

Sc4r


2 Answers

The line-extending backslash has the issue of having trailing whitespace that can break your code. This is a popular fix and is PEP8-compliant:

if (first_index < 0 or
    second_index > self._number_of_plates - 1):
like image 88
David Ehrmann Avatar answered Oct 20 '22 05:10

David Ehrmann


A continuation line is indented farther than it should be for a visual indent.

Anti-pattern In this example, the string "World" is indented two spaces farther than it should be.

print("Python", ("Hello",
                   "World"))

Best practice

print("Python", ("Hello",
                 "World"))

reference: https://www.flake8rules.com/rules/E127.html

like image 30
hassanzadeh.sd Avatar answered Oct 20 '22 05:10

hassanzadeh.sd