如何打破此Python行以保持在PEP 8的79个字符的限制内?
config["network"]["connection"]["client_properties"]["service"] = config["network"]["connection"]["client_properties"]["service"].format(service=service)
答案 0 :(得分:11)
考虑到Python可与引用一起使用,您可以执行以下操作:
<!-- the following is a Razor component, but I'm looking for
any way to include the **contents** of the svg file inline -->
<Image Source="myImage.svg" Inline="true" />
<!-- ==> Would output svg tag with contens ==> <svg id="blah" viewBox="0 0 75 50">...</svg> -->
答案 1 :(得分:8)
使用\
:
config["network"]["connection"]["client_properties"]["service"] = \
config["network"]["connection"]["client_properties"]["service"].format(
service=service)
答案 2 :(得分:2)
使用black,自以为是的可复制代码格式化程序:
config["network"]["connection"]["client_properties"][
"service"
] = config["network"]["connection"]["client_properties"][
"service"
].format(
service=service
)
答案 3 :(得分:2)
方括号允许隐式连续行。例如,
config["network"
]["connection"
]["client_properties"
]["service"] = config["network"]["connection"]["client_properties"]["service"].format(
service=service)
那就是说,我不认为每个括号应该在哪一行上达成共识。 (就我个人而言,我从未找到过看起来特别“正确”的任何选择。)
更好的解决方案可能是引入一个临时变量。
d = config["network"]["connection"]["client_properties"]
d["service"] = d["service"].format(service=service)
答案 4 :(得分:1)
您也可以使用变量来更好地阅读:
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
答案 5 :(得分:-2)
其他人试图将其分解为两行,但您的问题是它仍然很冗长。您可以将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