正如PEP8所建议的那样,在python程序中保持低于80列的规则,对于长字符串,我怎么能遵守这个规则呢?
s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."
我该如何把它扩展到下面一行呢?
s = "this is my really, really, really, really, really, really" +
"really long string that I'd like to shorten."
我倾向于使用几个这里没有提到的方法来指定大字符串,但这些方法适用于非常特定的场景。YMMV……
Multi-line blobs of text, often with formatted tokens (not quite what you were asking, but still useful):
error_message = '''
I generally like to see how my helpful, sometimes multi-line error
messages will look against the left border.
'''.strip()
Grow the variable piece-by-piece through whatever string interpolation method you prefer:
var = 'This is the start of a very,'
var = f'{var} very long string which could'
var = f'{var} contain a ridiculous number'
var = f'{var} of words.'
Read it from a file. PEP-8 doesn't limit the length of strings in a file; just the lines of your code. :)
Use brute-force or your editor to split the string into managaeble lines using newlines, and then remove all newlines. (Similar to the first technique I listed):
foo = '''
agreatbigstringthatyoudonotwanttohaveanyne
wlinesinbutforsomereasonyouneedtospecifyit
verbatimintheactualcodejustlikethis
'''.replace('\n', '')
我倾向于使用几个这里没有提到的方法来指定大字符串,但这些方法适用于非常特定的场景。YMMV……
Multi-line blobs of text, often with formatted tokens (not quite what you were asking, but still useful):
error_message = '''
I generally like to see how my helpful, sometimes multi-line error
messages will look against the left border.
'''.strip()
Grow the variable piece-by-piece through whatever string interpolation method you prefer:
var = 'This is the start of a very,'
var = f'{var} very long string which could'
var = f'{var} contain a ridiculous number'
var = f'{var} of words.'
Read it from a file. PEP-8 doesn't limit the length of strings in a file; just the lines of your code. :)
Use brute-force or your editor to split the string into managaeble lines using newlines, and then remove all newlines. (Similar to the first technique I listed):
foo = '''
agreatbigstringthatyoudonotwanttohaveanyne
wlinesinbutforsomereasonyouneedtospecifyit
verbatimintheactualcodejustlikethis
'''.replace('\n', '')
因为相邻的字符串常量是自动连接的,你可以这样编码:
s = ("this is my really, really, really, really, really, really, "
"really long string that I'd like to shorten.")
注意没有加号,我在示例的格式后面添加了额外的逗号和空格。
就我个人而言,我不喜欢反斜杠,我记得在某个地方读到过,它的使用实际上是被反对的,而支持这种更明确的形式。记住“显式优于隐式”。
我认为反斜杠不太清楚,也不太有用,因为这实际上是转义换行符。如果需要的话,不能在它后面加上行结束注释。可以通过串接字符串常量来实现:
s = ("this is my really, really, really, really, really, really, " # comments ok
"really long string that I'd like to shorten.")
我使用谷歌搜索“python行长”,它返回PEP8链接作为第一个结果,但也链接到另一个关于这个主题的StackOverflow帖子:“为什么python PEP-8应该指定79个字符的最大行长?”
另一个好的搜索短语是“python line continuation”。
您丢失了一个空格,您可能需要一个行延续字符,即。一个\。
s = "this is my really, really, really, really, really, really" + \
" really long string that I'd like to shorten."
甚至:
s = "this is my really, really, really, really, really, really" \
" really long string that I'd like to shorten."
parns也可以代替行续符,但可能会有人认为您打算使用元组,而只是忘记了逗号。举个例子:
s = ("this is my really, really, really, really, really, really"
" really long string that I'd like to shorten.")
对比:
s = ("this is my really, really, really, really, really, really",
" really long string that I'd like to shorten.")
使用Python的动态类型,代码可以以任何一种方式运行,但会产生不正确的结果。
这些都是很好的答案,但我找不到一个编辑器插件,可以帮助我编辑“隐式连接”的字符串,所以我写了一个包,让我更容易。
在pip(安装段落)上,如果有人想查看这个旧线程的话。以html的方式格式化多行字符串(压缩空格,一个新段落有两个换行符,不用担心行间空格)。
from paragraphs import par
class SuddenDeathError(Exception):
def __init__(self, cause: str) -> None:
self.cause = cause
def __str__(self):
return par(
f""" Y - e - e - e - es, Lord love you! Why should she die of
{self.cause}? She come through diphtheria right enough the year
before. I saw her with my own eyes. Fairly blue with it, she
was. They all thought she was dead; but my father he kept ladling
gin down her throat till she came to so sudden that she bit the bowl
off the spoon.
What call would a woman with that strength in her have to die of
{self.cause}? What become of her new straw hat that should have
come to me? Somebody pinched it; and what I say is, them as pinched
it done her in."""
)
raise SuddenDeathError("influenza")
变得……
__main__.SuddenDeathError: Y - e - e - e - es, Lord love you! Why should she die of influenza? She come through diphtheria right enough the year before. I saw her with my own eyes. Fairly blue with it, she was. They all thought she was dead; but my father he kept ladling gin down her throat till she came to so sudden that she bit the bowl off the spoon.
What call would a woman with that strength in her have to die of influenza? What become of her new straw hat that should have come to me? Somebody pinched it; and what I say is, them as pinched it done her in.
一切都很容易与(Vim)对齐“gq”