Python PEP8 打印不缩进的换行字符串

发布于 2024-08-02 12:28:34 字数 1033 浏览 3 评论 0原文

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

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好听的两个字的网名 2024-08-09 12:28:34

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

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


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

Use automatic string concatenation + implicit line continuation:

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


>>> long_string
'Line 1 Line 2 Line 3 '
神也荒唐 2024-08-09 12:28:34

这是可行的:

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

虽然我会这样布局:(

if __name__=='__main__':
    usage = ('%prog [options]\n'
             'Without any options, will display 10 random users '
             'of each type.')
    parser = OptionParser(usage)

所以当字符串中有 \n 时,以及当我需要对源代码进行自动换行时,我会开始一个新行。)

This works:

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

Although I'd lay it out like this:

if __name__=='__main__':
    usage = ('%prog [options]\n'
             'Without any options, will display 10 random users '
             'of each type.')
    parser = OptionParser(usage)

(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.)

╰つ倒转 2024-08-09 12:28:34

试试这个:

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

try this:

if __name__=='__main__':
    usage = '%prog [options]\nWithout any options, will display 10 random ' \
    'users of each type.'
    parser = OptionParser(usage)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文