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)
Asked By: Maggyero



Using black, the opinionated, reproducible code formatter:

] = config["network"]["connection"]["client_properties"][
Answered By: Reblochon Masque

Use a :

config["network"]["connection"]["client_properties"]["service"] = 

You also could use a variable for better reading:

client_service = config["network"]["connection"]["client_properties"]["service"]
client_service = client_service.format(service=service)

# If you are using the value later in your code keeping it in an variable may
# increase readability
# else you can put it back
config["network"]["connection"]["client_properties"]["service"] = client_service
Answered By: Kevin Müller

The brackets permit implicit line continuation. For example,

]["service"] = config["network"]["connection"]["client_properties"]["service"].format(

That said, I don’t think there’s any consensus as to which line each bracket should go on. (Personally, I’ve never found any choice that looks particularly “right”.)

A better solution would probably be to introduce a temporary variable.

d = config["network"]["connection"]["client_properties"]
d["service"] = d["service"].format(service=service)
Answered By: chepner

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)
Answered By: Yevhen Kuzmovych