python end用法在固定位置换行_在Python中链接方法时,换行的样式正确

PEP 8建议使用括号,这样您就不需要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),并轻轻地建议在二进制运算符之前而不是在二进制运算符之后进行分隔。 因此,格式化代码的首选方式是这样的:

my_var = (somethinglikethis

.where(we=do_things)

.where(we=domore)

.where(we=everdomore))

最大行长部分中的两个相关段落是:

包装长行的首选方法是在括号,方括号和花括号内使用Python的隐含行连续性。 通过将表达式包装在括号中,可以将长行分成多行。 应优先使用这些,而不是使用反斜线进行行连续。

...以及整个行在二进制运算符之前或之后应该换行吗? 部分:

换行符应该在二进制运算符之前还是之后?

几十年来,推荐的样式是在二进制运算符之后使用。但这会从两个方面损害可读性:操作员倾向于分散在屏幕上的不同列中,每个运算符从其操作数移至上一行。 在这里,眼睛必须做额外的工作才能知道添加了哪些项目,哪些是减去:

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)

为了解决此可读性问题,数学家及其出版商遵循相反的约定。 Donald Knuth解释了传统他的“计算机和排版”系列中的一条规则是:“尽管公式段落中的内容总是在二进制运算和关系之后中断,显示的公式总是在二进制运算之前中断”

遵循数学的传统通常会带来更多可读代码:

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)

在Python代码中,允许在二进制文件之前或之后中断运算符,只要约定在本地是一致的。 对于新建议使用Knuth的样式。

请注意,如上面引文所述,PEP 8曾经就在何处中断运算符给出了相反的建议,以下为后人引述:

包装长行的首选方法是使用Python的隐含行 括号,方括号和大括号内的延续。 长线可以 通过用括号括起来的表达式将多行分开。 这些 应优先使用反斜线进行行连续。 确保适当缩进续行。 首选地点 打破二元运算符是在运算符之后,而不是在运算符之前。 一些例子:

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值