为了服从python样式规则,我将编辑器设置为最多79列。Python风格 - 字符串延续?
在PEP中,它建议在括号,圆括号和大括号中使用python的隐含延续。但是,当我遇到字符串限制时处理字符串时,会变得有点奇怪。
例如,试图用一个多
mystr = """Why, hello there
wonderful stackoverflow people!"""
将返回
"Why, hello there\nwonderful stackoverflow people!"
这工作:
mystr = "Why, hello there \
wonderful stackoverflow people!"
因为它返回:
"Why, hello there wonderful stackoverflow people!"
但是,当语句中缩进几个街区,这看起来奇怪:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
如果你试图缩进第二行:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there \
wonderful stackoverflow people!"
你的字符串结束为:
"Why, hello there wonderful stackoverflow people!"
我发现解决这个问题的唯一方法是:
do stuff:
and more stuff:
and even some more stuff:
mystr = "Why, hello there" \
"wonderful stackoverflow people!"
我喜欢哪一种更好,但眼睛也有点不安,因为它看起来像是坐在无处不在的中间。这将产生正确的:
"Why, hello there wonderful stackoverflow people!"
所以,我的问题是 - 什么是不显示我应该怎么做这个有些人对如何做到这一点,是有什么我失踪的风格指南中的建议?
谢谢。
高缩进级别也可能是你需要重构代码,以便它更模块化 – Daenyth 2011-03-25 20:18:38
我缩进那么多做出点的标志。但是要意识到,至少可以达到第三级缩进是很容易的,但是即使只有一个缩进级别,标准方法也会使字符串变得非常不合适。 – sjmh 2011-03-25 20:30:35
可能重复的[在Python中换行长行](http://stackoverflow.com/questions/3346230/wrap-long-lines-in-python) – JrBenito 2016-09-27 19:18:06