如何编写符合PEP8的超长字符串并防止E501 [英] How to write very long string that conforms with PEP8 and prevent E501

查看:37
本文介绍了如何编写符合PEP8的超长字符串并防止E501的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 PEP8 建议将 Python 程序的列规则保持在 80 列以下,我该如何使用长字符串来遵守该规则,即

As PEP8 suggests keeping below the 80 column rule for your python program, how can I abide to that with long strings, i.e.

s = "this is my really, really, really, really, really, really, really long string that I'd like to shorten."

我将如何将其扩展到以下行,即

How would I go about expanding this to the following line, i.e.

s = "this is my really, really, really, really, really, really" + 
    "really long string that I'd like to shorten."

推荐答案

隐式串联可能是最干净的解决方案:

Implicit concatenation might be the cleanest solution:

s = "this is my really, really, really, really, really, really," \
    " really long string that I'd like to shorten."

编辑 经过反思,我同意 Todd 的建议使用括号而不是行继续符,因为他给出的所有原因都更好.我唯一的犹豫是将括号内的字符串与元组混淆相对容易.

Edit On reflection I agree that Todd's suggestion to use brackets rather than line continuation is better for all the reasons he gives. The only hesitation I have is that it's relatively easy to confuse bracketed strings with tuples.

这篇关于如何编写符合PEP8的超长字符串并防止E501的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆