偶然有次機會,看到同事在一串代碼上方加了這樣一行注釋:鏈式呼叫,現在才想起來,特意去了解了一下,
方法鏈:本質是在每個方法末尾都回傳實體本身,實作連續自身狀態或者行為的訪問
1 class Valley: 2 3 @property 4 def name(self): 5 print("g") 6 return self 7 8 def read(self): 9 print("reading") 10 # 回傳實體 11 return self 12 13 def write(self): 14 print("writing") 15 return self 16 17 18 if __name__ == '__main__': 19 Valley().name.read().write()
只有永不遏止的奮斗,才能使青春之花,即便是凋謝,也是壯麗地凋謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/549111.html
標籤:Python
上一篇:Java筆記(8) 例外和錯誤
