正如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', '')
使用黑色[https://github.com/psf/black],我像这样格式化它。
help=f"""filters, lista de filtros para cargar las base de conocimiento.
Pueden mandarse solo algunos filtros ya que no son obligatorios,
por ejemplo, si no se manda sts, se cargarán todos las bases de todos los estados.""",
您丢失了一个空格,您可能需要一个行延续字符,即。一个\。
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的动态类型,代码可以以任何一种方式运行,但会产生不正确的结果。