简体   繁体   中英

Python3.2 vertical chaining

In Python3.2 I can do this:

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

Eventually the chain becomes pretty long. I have an itch for a vertical chaining.

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

Is there any way to do that?

Just enclose your expression in parenthesis:

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

Thank you @Krotton for the answer it works indeed. Also thanks to @sean for the link . So, the correct way to use vertical chaining is:

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

You also may use the syntax, like with multi-line strings, to allow vertical chaining:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM