0

I'm trying to access the values of a python dictionary, but the line is too long so it doesn't match PEP-8 rules. (I'm using flake8 linter on vscode)

example:

class GoFirstSpider():
    def __init__(self, flight_search_request):
        self.name = 'goFirst'
     -> self.date = flight_search_request["FlightSearchRequest"]["FlightDetails"]["DepartureDate"]

I've tried:

self.date = flight_search_request["FlightSearchRequest"]\
    ["FlightDetails"]["DepartureDate"]

and got:

whitespace before '['

Thanks.

  • Hello! Can you post a example input and expected output? Thanks – dotheM Nov 24 '22 at 11:56
  • Slash `\ ` lets you break the line and continue in another. You can put it inbetween `]` and `[` for example. – matszwecja Nov 24 '22 at 11:56
  • „and got:“ How? This is not a Python error. Does some specific tool, say an IDE, report that? – MisterMiyagi Nov 24 '22 at 12:25
  • @MisterMiyagi yes, it's a flake8 error. – Daniel Avigdor Nov 24 '22 at 12:27
  • I don't see such an [error for pycodestyle](https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes) (which is used by flake8). Can you provide the exact error message? – MisterMiyagi Nov 24 '22 at 12:40
  • 1
    [{ "resource": "/c:/Projects/amscraper-3/src/flights_cadger/gofirst_spider.py", "owner": "python", "code": "E211", "severity": 8, "message": "whitespace before '['", "source": "flake8", "startLineNumber": 9, "startColumn": 65, "endLineNumber": 9, "endColumn": 65 }] – Daniel Avigdor Nov 24 '22 at 13:10
  • Thanks. I have updated the duplicate list to one Q&A explicitly addressing this. – MisterMiyagi Nov 24 '22 at 13:18

2 Answers2

0

You can use \ to break lines.

class GoFirstSpider():
    def __init__(self, flight_search_request):
        self.name = 'goFirst'
        self.date = flight_search_request["FlightSearchRequest"] \
        ["FlightDetails"]["DepartureDate"]
Jeremy Savage
  • 944
  • 5
  • 14
0

You can use \ to break multiple lines as stated in the PEP 8 style guide:

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 could not use implicit continuation before Python 3.10, so backslashes were acceptable for that case

In your case:

class GoFirstSpider():
    def __init__(self, flight_search_request):
        self.name = 'goFirst'
        self.date = flight_search_request["FlightSearchRequest"] \
            ["FlightDetails"]["DepartureDate"]
CcmU
  • 780
  • 9
  • 22