簡體   English   中英

Python3.2垂直鏈接

[英]Python3.2 vertical chaining

在Python3.2中,我可以這樣做:

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

最終,鏈條變得很長。 我對垂直鏈接很感興趣。

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

有什么辦法嗎?

只需將表達式括在括號中:

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

謝謝@Krotton的回答,它確實有效。 也感謝@sean的鏈接 因此,使用垂直鏈接的正確方法是:

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

您也可以使用語法(如多行字符串)來允許垂直鏈接:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM