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)
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.
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.
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.
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.
Use a \
:
config["network"]["connection"]["client_properties"]["service"] = \
config["network"]["connection"]["client_properties"]["service"].format(
service=service
)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With