函数中Python多行字符串的缩进方式是怎样的?
def method():
string = """line one
line two
line three"""
or
def method():
string = """line one
line two
line three"""
还是别的什么?
在第一个例子中,将字符串挂在函数外面看起来有点奇怪。
函数中Python多行字符串的缩进方式是怎样的?
def method():
string = """line one
line two
line three"""
or
def method():
string = """line one
line two
line three"""
还是别的什么?
在第一个例子中,将字符串挂在函数外面看起来有点奇怪。
当前回答
更多的选择。在启用了pylab的Ipython中,dedent已经在名称空间中。我检查了一下,它来自matplotlib。或者可以导入:
from matplotlib.cbook import dedent
在文档中,它指出它比文本封装的等效程序要快,在我用ipython进行的快速测试中,它的平均速度确实快3倍。它还有一个好处,它丢弃了任何前导空行,这让你可以灵活地构造字符串:
"""
line 1 of string
line 2 of string
"""
"""\
line 1 of string
line 2 of string
"""
"""line 1 of string
line 2 of string
"""
在这三个示例上使用matplotlib依赖项将得到相同的合理结果。textwrap dedent函数将在第一个示例中有一个前导空行。
明显的缺点是textwrap在标准库中,而matplotlib是外部模块。
这里有一些权衡……在定义字符串的地方,dedent函数使代码更具可读性,但需要稍后处理以获得可用格式的字符串。在文档字符串中,很明显应该使用正确的缩进,因为大多数使用文档字符串都会执行所需的处理。
当我需要一个非长字符串在我的代码中,我发现以下承认丑陋的代码,我让长字符串退出封闭缩进。“美丽总比丑陋好”这句话绝对不合适,但有人可能会说,它比抑扬抑扬的说法更简单、更明确。
def example():
long_string = '''\
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip.\
'''
return long_string
print example()
其他回答
更多的选择。在启用了pylab的Ipython中,dedent已经在名称空间中。我检查了一下,它来自matplotlib。或者可以导入:
from matplotlib.cbook import dedent
在文档中,它指出它比文本封装的等效程序要快,在我用ipython进行的快速测试中,它的平均速度确实快3倍。它还有一个好处,它丢弃了任何前导空行,这让你可以灵活地构造字符串:
"""
line 1 of string
line 2 of string
"""
"""\
line 1 of string
line 2 of string
"""
"""line 1 of string
line 2 of string
"""
在这三个示例上使用matplotlib依赖项将得到相同的合理结果。textwrap dedent函数将在第一个示例中有一个前导空行。
明显的缺点是textwrap在标准库中,而matplotlib是外部模块。
这里有一些权衡……在定义字符串的地方,dedent函数使代码更具可读性,但需要稍后处理以获得可用格式的字符串。在文档字符串中,很明显应该使用正确的缩进,因为大多数使用文档字符串都会执行所需的处理。
当我需要一个非长字符串在我的代码中,我发现以下承认丑陋的代码,我让长字符串退出封闭缩进。“美丽总比丑陋好”这句话绝对不合适,但有人可能会说,它比抑扬抑扬的说法更简单、更明确。
def example():
long_string = '''\
Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip.\
'''
return long_string
print example()
我更喜欢
def method():
string = \
"""\
line one
line two
line three\
"""
or
def method():
string = """\
line one
line two
line three\
"""
我来这里寻找一个简单的1-liner来删除/纠正打印文档字符串的标识级别,而不会使它看起来不整洁,例如通过使它在脚本中“悬挂在函数之外”。
这是我最后做的:
import string
def myfunction():
"""
line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print str(string.replace(myfunction.__doc__,'\n\t','\n'))[1:]
显然,如果你使用空格缩进(例如4)而不是制表键,可以使用这样的方法:
print str(string.replace(myfunction.__doc__,'\n ','\n'))[1:]
如果你想让你的文档字符串看起来像这样,你不需要删除第一个字符:
"""line 1 of docstring
line 2 of docstring
line 3 of docstring"""
print string.replace(myfunction.__doc__,'\n\t','\n')
对于字符串,可以直接处理字符串。对于文档字符串,您需要在之后处理函数。这里有一个解决方案,这两个仍然是可读的。
class Lstrip(object):
def __rsub__(self, other):
import re
return re.sub('^\n', '', re.sub('\n$', '', re.sub('\n\s+', '\n', other)))
msg = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
''' - Lstrip()
print msg
def lstrip_docstring(func):
func.__doc__ = func.__doc__ - Lstrip()
return func
@lstrip_docstring
def foo():
'''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
'''
pass
print foo.__doc__
你可能想和""对齐
def foo():
string = """line one
line two
line three"""
由于换行符和空格包含在字符串本身中,因此必须对其进行后处理。如果你不想这样做,而你有很多文本,你可能想把它们单独存储在一个文本文件中。如果一个文本文件不适合你的应用程序,而你不想进行后期处理,我可能会选择
def foo():
string = ("this is an "
"implicitly joined "
"string")
如果你想对一个多行字符串进行后处理,去掉你不需要的部分,你应该考虑textwrap模块或PEP 257中提供的后处理文档字符串的技术:
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxint
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)