【发布时间】:2011-03-25 20:12:37
【问题描述】:
为了遵守 python 样式规则,我将编辑器设置为最多 79 列。
在 PEP 中,它建议在括号、圆括号和大括号内使用 python 的隐含延续。但是,当我达到 col 限制时处理字符串时,它会变得有点奇怪。
例如,尝试使用多行
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!"
所以,我的问题是 - 有些人对如何做到这一点有什么建议,我在风格指南中是否遗漏了一些东西来说明我应该如何做到这一点?
谢谢。
【问题讨论】:
-
我缩进了这么多来说明一点。但是要意识到至少达到第三级缩进是很容易的——但情况仍然是,即使只有一级缩进,标准方法也会使字符串非常不合适。
标签: python coding-style