Python的换行符换行符 [英] Correct Style for Python Line breaks

查看:289
本文介绍了Python的换行符换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的代码。

 #before 
my_var = somethinglikethis.where(we = do_things) 。where(we = domore).where(we = everdomore)

#this way
my_var = somethinglikethis.where(we = do_things)\
.where domore)\
.where(we = everdomore)

#或这种方式
my_var = somethinglikethis.where(we = do_things)。 \
其中(we = domore)。 \
其中(we = everdomore)


解决方案

个人而言,我更喜欢使用括号,因此您不需要 \

  my_var =(somethinglikethis 
.where(we = do_things)
.where(we = domore)
.where(we = everdomore))



(我更改了缩进,因为这是我的编辑器自动缩进的缩写。)



我只是通过PEP 8,看起来没有提到换行。
但是括号(或其他字符,例如 [] {} )应该有助于您的编辑缩进





这里是风格指南所说的感谢delnan:


包裹长行的首选方法是使用Python的隐含line
括号内的括号,括号和大括号。长行可以通过在括号中包装表达式来
折断多行。这些
应该优先使用反斜杠线连续。
确保适当地缩进连续行。优先放置
来绕过二进制运算符是之后运算符,而不是它之前。
一些例子:

  class Rectangle(Blob):

def __init __(self, width,height,
color ='black',emphasis = None,highlight = 0):
if(width == 0 and height == 0 and
color =='red'and强调=='strong'或
加亮> 100):
raise ValueError(sorry,you lose)
如果width == 0和height == 0, 'red'or
emphasis is None):
raise ValueError(我不这么认为 - 值是%s,%s%
(width,height))
Blob .__ init __(self,width,height,
color,emphasis,highlight)




风格指南现在建议在二进制运算符之前b

https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator


I have some code like this. Should the break occur before the periods or after?

# before
my_var = somethinglikethis.where(we=do_things).where(we=domore).where(we=everdomore)

# this way
my_var = somethinglikethis.where(we=do_things) \
                          .where(we=domore) \
                          .where(we=everdomore)

# or this way
my_var = somethinglikethis.where(we=do_things). \
                           where(we=domore). \
                           where(we=everdomore)

解决方案

Personally, I prefer using parenthesis so that you don't need \:

my_var = (somethinglikethis
          .where(we=do_things)
          .where(we=domore)
          .where(we=everdomore))

(I changed the indentation because that's how my editor indents it automatically.)

I just looked through PEP 8, and it looks like there's no mention of line breaks. But parenthesis (or other characters such as [] or {}) should help your editor indent the code automatically, and they only require you to modify the beginning and the end of the expression.

[Edit]

Here is what the style guide says about it, thanks to delnan:

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. Some examples:

class Rectangle(Blob):

    def __init__(self, width, height,
                 color='black', emphasis=None, highlight=0):
        if (width == 0 and height == 0 and
            color == 'red' and emphasis == 'strong' or
            highlight > 100):
            raise ValueError("sorry, you lose")
        if width == 0 and height == 0 and (color == 'red' or
                                           emphasis is None):
            raise ValueError("I don't think so -- values are %s, %s" %
                             (width, height))
        Blob.__init__(self, width, height,
                      color, emphasis, highlight)

[Edit]

The style guide now recommends breaking before a binary operator (thanks @Neapolitan):

https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator

这篇关于Python的换行符换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆