Function chaining in Python

前端 未结 6 1519
醉酒成梦
醉酒成梦 2020-11-27 11:17

On Codewars.com I encountered the following task:

Create a function add that adds numbers together when called in succession. So add(1

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 11:43

    Simply:

    class add(int):
       def __call__(self, n):
          return add(self + n)
    

提交回复
热议问题