如何在不牺牲缩进的情况下在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陈述而导致缩进不均匀会很糟糕.
如何在不牺牲代码可读性的情况下有效地缩进这样的行?
由于PEP8建议保持低于你的python程序的80列规则,我怎么能遵守长字符串,即
s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud)
我将如何将其扩展到以下行,即
s = "this is my really, really, really, really, really, really" +
"really long string that I'd like to shorten."
Run Code Online (Sandbox Code Playgroud) 我正在Emacs中编写一个Python脚本,并激活了自动填充次要模式M-x auto-fill-mode.我似乎经常遇到的一个问题是,这种填充模式往往会破坏多行中的引用字符串而不进行任何补偿调整,从而导致脚本运行时出错.
例如:
print 'the quick brown fox jumped over the lazy dog and
then did something else'
Run Code Online (Sandbox Code Playgroud)
这导致SyntaxError: EOL while scanning string literal运行时.
在Emacs中是否存在填充模式,即Python"字符串文字识别",并自动进行Python样式中讨论的行继续/相关调整之一- 使用字符串继续行?而不是天真地分裂导致错误的字符串?
我无法在python编译器中将路径分成两行.它只是编译器屏幕上的一条长路径,我必须将窗口拉得太宽.我知道如何将打印("字符串")分成两行代码,这些代码将正确编译,但不能打开(路径).当我写这篇文章时,我注意到文本框甚至无法将它全部保存在一行上.打印()
`raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles\test code\rawstringfiles.txt', 'a')`
Run Code Online (Sandbox Code Playgroud) 我在我的python代码中每行严格强制执行80个字符,但偶尔我必须将一行字符串分成两行,如下所示
print time.strftime("%m/%d/%Y %I:%M:%S \
%p",time.localtime(os.path.getmtime(fname)))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,输出将在"AM/PM"前面有额外的空格,如
foo.dat was last modified: 01/10/2012 02:53:15 AM
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法来解决这个问题,只是为了避免切断弦,或者在这种情况下允许长线,但是,我想知道是否还有其他更多的内在方法来解决这个问题,同时允许str-cutting和line-breaking发生.第二行由编辑器自动缩进,我也不想改变它.谢谢.