在 python 中,垂直方向的字符串列表很常见。例如:
subprocess.check_output( [
'application',
'-first-flag',
'-second-flag',
'-some-additional-flag'
] )
这看起来不错,可读,不违反 80 列规则...但是如果缺少逗号,如下所示:
subprocess.check_output( [
'application',
'-first-flag' # missed comma here
'-second-flag',
'-some-additional-flag'
] )
Python 仍然会通过连接两个字符串来假定此代码有效:(。是否有可能以某种方式保护自己免受此类拼写错误的影响,同时仍然使用垂直方向的字符串列表并且没有膨胀的代码(例如将每个项目封装在里面)str()
)?
您可以将每个字符串括在括号中:
subprocess.check_output( [
('application'),
('-first-flag'),
('-second-flag'),
('-some-additional-flag'),
] )
顺便说一句,Python 可以使用尾随逗号,因此始终在行尾使用逗号,这也应该可以减少错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)