2012-12-03 60 views
2

在Python3.2我可以这样做:Python3.2垂直链接

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

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

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

有没有办法做到这一点?

+0

http://stackoverflow.com/questions/2550439 /在多行上修改正确的方式对多功能调用 – sean

+0

http://stackoverflow.com/questions/53162/how-can-i-do-a-line-break -line-continuation-in-python – asheeshr

+0

谢谢各位的链接。自从我用关键词“chaining”查找时,找不到任何这些问题:) – nkamm

回答

2

只需附上表达括号:

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

谢谢@Krotton为它工作确实的答案。还要感谢@sean的link。因此,使用垂直链接正确的做法是:

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

您也可以使用的语法,像多行字符串,允许垂直链接:

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