JasonGenX
JasonGenX

Reputation: 5434

PEP 8 E211 issue raised. Not sure why

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?

Upvotes: 6

Views: 2549

Answers (1)

NickAb
NickAb

Reputation: 6687

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:

Upvotes: 7

Related Questions