本文介绍了Python PEP8 打印没有缩进的包装字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此可能有一个简单的答案,只是不知道如何从我的搜索中梳理出来.

There is probably an easy answer for this, just not sure how to tease it out of my searches.

我在我的 python 代码中遵守 PEP8,并且我目前正在将 OptionParser 用于我正在编写的脚本.为了防止行超过 80,我在需要的地方使用反斜杠.

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.

例如:

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.

随机"之后的那个差距让我感到困扰.我可以这样做:

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)

一定有更好的方法吗?

推荐答案

使用 自动字符串连接 + 隐式续行:

long_string = ("Line 1 "
               "Line 2 "
               "Line 3 ")


>>> long_string
'Line 1 Line 2 Line 3 '

这篇关于Python PEP8 打印没有缩进的包装字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 00:50