如何在 Python 中打破这条长线?

2022-01-12 00:00:00 python formatting code-formatting

问题描述

您将如何格式化这样的长行?我希望它的宽度不超过 80 个字符:

How would you go about formatting a long line such as this? I'd like to get it to no more than 80 characters wide:

logger.info("Skipping {0} because its thumbnail was already in our system as {1}.".format(line[indexes['url']], video.title))

这是我最好的选择吗?

url = "Skipping {0} because its thumbnail was already in our system as {1}."
logger.info(url.format(line[indexes['url']], video.title))


解决方案

这是一个开始.在使用它们的代码之外定义较长的字符串并不是一个坏习惯.这是一种分离数据和行为的方法.您的第一个选择是通过使它们彼此相邻来隐式地将字符串文字连接在一起:

That's a start. It's not a bad practice to define your longer strings outside of the code that uses them. It's a way to separate data and behavior. Your first option is to join string literals together implicitly by making them adjacent to one another:

("This is the first line of my text, "
"which will be joined to a second.")

或者使用行尾延续,这有点脆弱,因为这样可以:

Or with line ending continuations, which is a little more fragile, as this works:

"This is the first line of my text, " 
"which will be joined to a second."

但这不是:

"This is the first line of my text, "  
"which will be joined to a second."

看到区别了吗?不?好吧,当它是您的代码时,您也不会.

See the difference? No? Well you won't when it's your code either.

隐式连接的缺点是它只适用于字符串文字,而不适用于从变量,所以当你重构的时候事情会变得更加复杂.此外,您只能在整个组合字符串上插入格式.

The downside to implicit joining is that it only works with string literals, not with strings taken from variables, so things can get a little more hairy when you refactor. Also, you can only interpolate formatting on the combined string as a whole.

或者,您可以使用连接运算符 (+) 显式连接:

Alternatively, you can join explicitly using the concatenation operator (+):

("This is the first line of my text, " + 
"which will be joined to a second.")

显式优于隐式,正如 python 的禅宗所说,但这会创建三个字符串而不是一个,并使用两倍的内存:有你写的两个,加上一个是它们中的两个连接在一起,所以你必须知道什么时候忽略禅.好处是您可以将格式应用于每行单独的任何子字符串,或括号外的全部.

Explicit is better than implicit, as the zen of python says, but this creates three strings instead of one, and uses twice as much memory: there are the two you have written, plus one which is the two of them joined together, so you have to know when to ignore the zen. The upside is you can apply formatting to any of the substrings separately on each line, or to the whole lot from outside the parentheses.

最后,您可以使用三引号字符串:

Finally, you can use triple-quoted strings:

"""This is the first line of my text
which will be joined to a second."""

这通常是我最喜欢的,尽管它的行为略有不同,因为换行符和后续行中的任何前导空格都会显示在您的最终字符串中.您可以使用转义反斜杠消除换行符.

This is often my favorite, though its behavior is slightly different as the newline and any leading whitespace on subsequent lines will show up in your final string. You can eliminate the newline with an escaping backslash.

"""This is the first line of my text 
which will be joined to a second."""

这与上述相同技术具有相同的问题,因为正确代码与错误代码的区别仅在于不可见的空格.

This has the same problem as the same technique above, in that correct code only differs from incorrect code by invisible whitespace.

哪个是最好的"取决于你的具体情况,但答案不仅仅是审美,而是一种微妙不同的行为.

Which one is "best" depends on your particular situation, but the answer is not simply aesthetic, but one of subtly different behaviors.

相关文章