Re: [問題] +=問題
前文 43
→
05/15 23:29,
05/15 23:29
→
05/15 23:30,
05/15 23:30
推
05/16 08:45,
05/16 08:45
→
05/16 08:58,
05/16 08:58
→
05/16 08:59,
05/16 08:59
→
05/16 09:00,
05/16 09:00
in-place 的 method 是可以回傳原值的,只要重載 += 的 protocol __iadd__ 的
時候 return self 就可以了。((但我回文不是為了講這個...
在 OOP 裡,這被稱為 Method chaining (in-place 修改完後會回傳原值是因為這
個 idiom), Python 雖然也是 OOP 語言,但 Guido 對於這個 idiom 有不同的立
場(註1),所以 in-place 在 Python 的實作基本上都是回傳 None 。
註:
1. https://mail.python.org/pipermail/python-dev/2003-October/038855.html
2. 開始學 FP 之後變得蠻認同這點的。除了可讀性較高外,這跟 explict self
一樣具有提醒的作用。明確的考量 mutable/immutable 跟 side-effect 比起
方便的 method chaining 來得優雅許多。((個人意見QQ
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 192.19.253.250
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1494910279.A.8D9.html
討論串 (同標題文章)