如何在不牺牲缩进的情况下在Python中包装长行?
例如:
def fun():
print '{0} Here is a really long sentence with {1}'.format(3, 5)
Run Code Online (Sandbox Code Playgroud)
假设这超过了79个字符的建议限制.我读它的方式,这里是如何缩进它:
def fun():
print '{0} Here is a really long \
sentence with {1}'.format(3, 5)
Run Code Online (Sandbox Code Playgroud)
但是,使用这种方法,连续行的缩进与缩进相匹配fun()
.这看起来有点难看.如果有人要查看我的代码,那么由于这个print
陈述而导致缩进不均匀会很糟糕.
如何在不牺牲代码可读性的情况下有效地缩进这样的行?
小智 263
def fun():
print(('{0} Here is a really long '
'sentence with {1}').format(3, 5))
Run Code Online (Sandbox Code Playgroud)
Adjecent字符串常量是在编译时串连,就像在C. http://docs.python.org/reference/lexical_analysis.html#string-literal-concatenation是开始更多信息的好地方.
Ioa*_*dis 73
有两种方法没有在上面提到,但它们都以符合PEP 8 的方式解决问题,并允许您更好地利用空间.他们是:
msg = (
'This message is so long, that it requires '
'more than {x} lines.{sep}'
'and you may want to add more.').format(
x=x, sep=2*'\n')
print(msg)
Run Code Online (Sandbox Code Playgroud)
注意如何使用括号来允许我们不在纯字符串之间添加加号,并将结果分布在多行上,而不需要显式行继续'\'(丑陋和杂乱).优点与下面描述的相同,不同之处在于你可以在任何地方进行.与之前的替代方案相比,在检查代码时在视觉上更好,因为它msg
清楚地概述了开始和结束(与msg +=
每一行比较,需要一个额外的思考步骤来推断这些行添加到相同的字符串 - 以及如果你打错了,忘掉+
一条随机的线?).
关于这种方法,很多时候我们必须使用迭代体内的迭代和检查来构建一个字符串,因此在函数调用中添加它的片段,如下所示,不是一个选项.
一个接近的选择是:
msg = 'This message is so long, that it requires '
msg += 'many lines to write, one reason for that\n'
msg += 'is that it contains numbers, like this '
msg += 'one: ' + str(x) +', which take up more space\n'
msg += 'to insert. Note how newlines are also included '
msg += 'and can be better presented in the code itself.'
print(msg)
Run Code Online (Sandbox Code Playgroud)
虽然第一个是优选的.
另一种方法与之前的方法类似,但它在下面的行上启动消息print
.这样做的原因是为了在左边获得空间,否则它print(
本身会"推"你到右边.这种缩进的消耗是由构成消息的其余行继承的,因为根据PEP 8,它们必须与print
它们上面的左括号对齐.因此,如果你的信息已经很长,那么它就会被迫传播到更多的线路上.
对比:
raise TypeError('aaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaa')
Run Code Online (Sandbox Code Playgroud)
有了这个(这里建议):
raise TypeError(
'aaaaaaaaaaaaaaaaaaaaaaaa' +
'aaaaaaaaaaaaaaaaaaaaaaaa')
Run Code Online (Sandbox Code Playgroud)
线路传播减少了.当然,最后一种方法并不适用print
,因为这是一个短暂的呼叫.但它确实适用于例外情况.
您可以拥有的变体是:
raise TypeError((
'aaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaaaaaaaaaaaaaaaaaaaaa'
'aaaaa {x} aaaaa').format(x=x))
Run Code Online (Sandbox Code Playgroud)
注意你不需要在纯字符串之间加号.此外,缩进引导读者的眼睛,没有悬挂在左下方的流浪括号.替换是非常易读的.特别是,这种方法使得编写生成代码或数学公式的代码成为非常愉快的任务.
Sil*_*ost 22
您可以使用以下代码,缩进无关紧要:
>>> def fun():
return ('{0} Here is a really long'
' sentence with {1}').format(3, 5)
Run Code Online (Sandbox Code Playgroud)
您只需要在括号中包含字符串.
Chr*_* B. 19
您可以使用Python连接彼此相邻的字符串文字的事实:
>>> def fun():
... print '{0} Here is a really long ' \
... 'sentence with {1}'.format(3, 5)
Run Code Online (Sandbox Code Playgroud)
我可能会将长语句分成多个较短的语句,以便程序逻辑与长字符串的定义分开:
>>> def fun():
... format_string = '{0} Here is a really long ' \
... 'sentence with {1}'
... print format_string.format(3, 5)
Run Code Online (Sandbox Code Playgroud)
如果字符串只是太长而且您选择了一个简短的变量名称,那么通过这样做甚至可以避免分割字符串:
>>> def fun():
... s = '{0} Here is a really long sentence with {1}'
... print s.format(3, 5)
Run Code Online (Sandbox Code Playgroud)
我很惊讶没有人提到上面的隐式样式。我的偏爱是使用parens来包装字符串,同时将字符串线条视觉上对齐。我个人认为,这比在带标签的新行上开始字符串的开头看起来更干净,更紧凑。
请注意,这些括号不是方法调用的一部分-它们只是隐式字符串文字串联。
Python 2:
def fun():
print ('{0} Here is a really '
'long sentence with {1}').format(3, 5)
Run Code Online (Sandbox Code Playgroud)
Python 3(带有用于打印功能的括号):
def fun():
print(('{0} Here is a really '
'long sentence with {1}').format(3, 5))
Run Code Online (Sandbox Code Playgroud)
我个人认为将长字符串文字与打印字符串分开是最干净的:
def fun():
s = ('{0} Here is a really '
'long sentence with {1}').format(3, 5)
print(s)
Run Code Online (Sandbox Code Playgroud)