Python3.2垂直链接

时间:2012-12-03 14:49:19

标签: python syntax python-3.x

在Python3.2中,我可以这样做:

foo = Bar()
foo.setSomething(something1).setStatus('horizontal').setAttributes(attributes)

最终链条变得很长。我有一个垂直链接的痒。

foo = Bar()
foo.setSomething(something1)
   .setStatus('vertical')
   .setAttributes(attributes)

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

将表达式括在括号中:

foo = Bar()
(foo.setSomething(something1)
     .setStatus('vertical')
     .setAttributes(attributes))

答案 1 :(得分:2)

谢谢@Krotton的答案确实有效。还要感谢link的@sean。因此,使用垂直链接的正确方法是:

foo = Bar()
(foo.setSomething(something1)
     .setStatus('vertical')
     .setAttributes(attributes))

您也可以使用语法(如多行字符串)来允许垂直链接:

foo = Bar()
foo.setSomething(something1)\
   .setStatus('vertical')\
   .setAttributes(attributes)
相关问题