Python PEP8 打印不缩进的换行字符串
对此可能有一个简单的答案,只是不确定如何从我的搜索中剔除它。
我在 python 代码中遵守 PEP8 ,并且我目前正在使用 OptionParser对于我正在写的脚本。为了防止行数超过 80,我在需要时使用反斜杠。
例如:
if __name__=='__main__':
usage = '%prog [options]\nWithout any options, will display 10 random \
users of each type.'
parser = OptionParser(usage)
反斜杠后的缩进会导致:
~$ ./er_usersearch -h
Usage: er_usersearch [options]
Without any options, will display 10 random users of each type.
“随机”后的空白让我烦恼。我可以这样做:
if __name__=='__main__':
usage = '%prog [options]\nWithout any options, will display 10 random \
users of each type.'
parser = OptionParser(usage)
但这同样让我烦恼。这看起来很愚蠢:
if __name__=='__main__':
usage = ''.join(['%prog [options]\nWithout any options, will display',
' 10 random users of each type.'])
parser = OptionParser(usage)
一定有更好的方法吗?
There is probably an easy answer for this, just not sure how to tease it out of my searches.
I adhere to PEP8 in my python code, and I'm currently using OptionParser for a script I'm writing. To prevent lines from going beyond a with of 80, I use the backslash where needed.
For example:
if __name__=='__main__':
usage = '%prog [options]\nWithout any options, will display 10 random \
users of each type.'
parser = OptionParser(usage)
That indent after the backslash results in:
~$ ./er_usersearch -h
Usage: er_usersearch [options]
Without any options, will display 10 random users of each type.
That gap after "random" bugs me. I could do:
if __name__=='__main__':
usage = '%prog [options]\nWithout any options, will display 10 random \
users of each type.'
parser = OptionParser(usage)
But that bugs me just as much. This seems silly:
if __name__=='__main__':
usage = ''.join(['%prog [options]\nWithout any options, will display',
' 10 random users of each type.'])
parser = OptionParser(usage)
There must be a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 自动字符串连接 + 隐式续行:
Use automatic string concatenation + implicit line continuation:
这是可行的:
虽然我会这样布局:(
所以当字符串中有
\n
时,以及当我需要对源代码进行自动换行时,我会开始一个新行。)This works:
Although I'd lay it out like this:
(So I start a new line when there's a
\n
in the string, as well as when I need to word wrap the source code.)试试这个:
try this: