Search code examples
pythonpep8

Proper way of breaking line accessing dictionary


I am try to conform to pep8 directives and therefore to break the following line:

   config_data_dict['foo']['bar']['foobarfoo'] \
        ['barfoobar'] = something_else

However I am getting the following warning now just after the ['foobarfoo'] section

whitespace before '[' pep8(E211)

How should I properly break a line as the above (assuming I cannot brake it around =)?


Solution

  • Parentheses seem to work:

    (config_data_dict['foo']['bar']['foobarfoo']
     ['barfoobar']) = something_else
    

    This also seems to be the recommended style according to PEP8:

    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.