如何打破这条Python线?

问题描述 投票:0回答:6

如何打破此Python行以保持在PEP 8 79个字符的限制内?

config["network"]["connection"]["client_properties"]["service"] = config["network"]["connection"]["client_properties"]["service"].format(service=service)
python line-breaks pep8
6个回答
3
投票
使用\

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


1
投票
使用black,自以为是的,可复制的代码格式化程序:

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


1
投票
其他人试图将其分解为两行,但您的问题是它仍然非常冗长。您可以只将config["network"]["connection"]["client_properties"]["service"]分配给它自己的变量。]​​>

config_service = config["network"]["connection"]["client_properties"]["service"] config_service = config_service.format(service=service) # If you really need it at the original variable config["network"]["connection"]["client_properties"]["service"] = config_service


1
投票
方括号允许隐式连续行。例如,

0
投票
您也可以使用变量来更好地阅读:

0
投票
考虑到Python可与引用一起使用的事实,您可以执行以下操作:
© www.soinside.com 2019 - 2024. All rights reserved.