Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a long line with multiple bracket pairs?

How to break a long line with multiple bracket pairs to follow PEP 8’s 79-character limit?

config["network"]["connection"]["client_properties"]["service"] = config["network"]["connection"]["client_properties"]["service"].format(service=service)
like image 814
Maggyero Avatar asked Oct 02 '19 14:10

Maggyero


People also ask

How to split a long line in Python?

You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line.

How do you spread a long statement over multiple lines in Python?

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary, you can add an extra pair of parentheses around an expression, but sometimes using a backslash looks better.

How to interrupt a line in Python?

If you have a very long line of code in Python and you'd like to break it up over over multiple lines, if you're inside parentheses, square brackets, or curly braces you can put line breaks wherever you'd like because Python allows for implicit line continuation.

How do you shorten a line in Python?

The preferred way is by using Python's implied line continuation inside parentheses, brackets and braces. You can add an extra pair of parentheses around an expression if it is necessary, but sometimes using a backslash looks better. Also, make sure to indent the continued line appropriately.


2 Answers

Use a \:

config["network"]["connection"]["client_properties"]["service"] = \
    config["network"]["connection"]["client_properties"]["service"].format(
        service=service
    )
like image 42
Error - Syntactical Remorse Avatar answered Sep 24 '22 12:09

Error - Syntactical Remorse


Considering the fact that Python works with references you can do the following:

properties = config["network"]["connection"]["client_properties"]
properties["service"] = properties["service"].format(service=service)
like image 145
Yevhen Kuzmovych Avatar answered Sep 23 '22 12:09

Yevhen Kuzmovych