Python PEP8打印包裹的字符串没有缩进

EMi*_*ler 11 python pep8 word-wrap

对此可能有一个简单的答案,只是不确定如何从我的搜索中取出它.

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

例如:

if __name__=='__main__':
    usage = '%prog [options]\nWithout any options, will display 10 random \
    users of each type.'
    parser = OptionParser(usage)
Run Code Online (Sandbox Code Playgroud)

反斜杠后的缩进导致:

~$ ./er_usersearch -h
Usage: er_usersearch [options]
Without any options, will display 10 random     users of each type.
Run Code Online (Sandbox Code Playgroud)

"随机"之后的差距让我感到困惑.我可以:

 if __name__=='__main__':
    usage = '%prog [options]\nWithout any options, will display 10 random \
 users of each type.'
    parser = OptionParser(usage)
Run Code Online (Sandbox Code Playgroud)

但这让我感到困扰.这看起来很傻:

 if __name__=='__main__':
    usage = ''.join(['%prog [options]\nWithout any options, will display',
                     ' 10 random users of each type.'])
    parser = OptionParser(usage)
Run Code Online (Sandbox Code Playgroud)

肯定有更好的办法?

Tri*_*ych 28

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

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


>>> long_string
'Line 1 Line 2 Line 3 '
Run Code Online (Sandbox Code Playgroud)