Python3.2垂直链

2024-09-26 22:13:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python3.2中,我可以做到:

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

最后链条变得相当长。我渴望一条垂直的铁链。你知道吗

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

有什么办法吗?你知道吗


Tags: foobarattributesvertical办法horizontal链条something1
2条回答

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

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

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

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

只需将表达式括在括号中:

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

相关问题 更多 >

    热门问题