为了遵守Python的编码规则,我将我的编辑器设置为最大79列。
在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!"
所以,我的问题是 - 一些人对如何做这件事有什么建议,并且是否有样式指南中我应该遵循的内容被忽略了?
谢谢。