Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP 8 E211 issue raised. Not sure why

Tags:

python

My code:

if 'certainField' in myData['meta']['loc']:
   something = myData['meta'] \            <- PEP8 E11 raised for this
   ['loc'] \                               <- PEP8 E11 raised for this
   ['certainField'] \                      <- PEP8 E11 raised for this
   ['thefield']

The code works as expected. But PEP 8 E211 is raised for the second, third and fourth line claiming whitespace before '['

I don't get it. How can I format this so that PEP 8 is satisfied?

like image 947
JasonGenX Avatar asked Aug 22 '16 05:08

JasonGenX


1 Answers

You can wrap your statement into parenthesis and remove \

if 'certainField' in myData['meta']['loc']:
    something = (myData['meta']
                 ['loc']
                 ['certainField']
                 ['thefield'])


Here is an excerpt form PEP 8 on wrapping long 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.

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

like image 83
NickAb Avatar answered Oct 21 '22 07:10

NickAb