According to guido(以及对其他一些Python程序员来说),隐式字符串文本连接被认为是有害的。因此,我试图识别包含这种连接的逻辑行。在
我的第一次(也是唯一一次)尝试是使用shlex
;我想用posix=False
分割一条逻辑行,因此我将标识用引号封装的部分,如果这些部分相邻,则将被视为“文字连接”。在
但是,这在多行字符串上失败,如下例所示:
shlex.split('""" Some docstring """', posix=False)
# Returns '['""', '" Some docstring "', '""']', which is considered harmful, but it's not
我可以调整这是一些奇怪的特别的方式,但我想知道你是否能想出一个简单的解决办法。我的目的是将它添加到已经扩展的pep8
验证器中。在
我决定使用来自user2357112的建议,并对其进行一点扩展以获得以下解决方案,我在这里将其描述为
pep8
模块的扩展:更好地处理这一问题的一个方法是,当您有一个列表时,在右引号后面加一个空格(或两个):
现在更明显的是'three'缺少它的尾随逗号
建议:我建议python有一个pragma,表示在某个区域中禁止字符串-文字串联:
^{pr2}$默认情况下允许连接(以保持兼容性)
还有一条线索:
https://groups.google.com/forum/#!topic/python-ideas/jP1YtlyJqxs%5B1-25%5D
有趣的问题,我只是玩了一下,因为没有答案,所以我发布了我的解决方案:
你可以给程序提供它自己,结果应该是
^{pr2}$相关问题 更多 >
编程相关推荐