Function chaining in Python

前端 未结 6 1521
醉酒成梦
醉酒成梦 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:47

    The pythonic way to do this would be to use dynamic arguments:

    def add(*args):
        return sum(args)
    

    This is not the answer you're looking for, and you may know this, but I thought I would give it anyway because if someone was wondering about doing this not out of curiosity but for work. They should probably have the "right thing to do" answer.

提交回复
热议问题