加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 教授使用“函数的二进制版本”.这甚至存在吗?

发布时间:2020-12-20 12:07:32 所属栏目:Python 来源:网络整理
导读:我们的教授在作业中使用了这个.在Google中搜索它后,我不认为“函数的二进制版本”存在.你觉得这意味着什么? Say we have a function add that adds a bunch of numbers. Rather than writing add(3,5,4,1) we want to use currying to create an adder func
我们的教授在作业中使用了这个.在Google中搜索它后,我不认为“函数的二进制版本”存在.你觉得这意味着什么?

Say we have a function add that adds a bunch of numbers. Rather than
writing add(3,5,4,1) we want to use currying to create an adder
function that can be extended using a chain of calls. We would then
have adder(3)(5)(4)(1)(). Let us assume we have the currying function
that can create this adder given the add2 function (the binary version
of add
) and a start value. Let us call it curry. Then we have adder =
curry(add2,0).

解决方法

在这种情况下,“binary function”引用一个接受两个参数的参数.在这种情况下,你的教授可能指的是这样的事情:

def add2(x,y):
    return x + y
    # equivalently: add2 = lambda x,y: x+y

def curry(func,num):
    def wrapped(*args):
        if len(args) == 0:
            return num
        elif len(args) > 1:
            raise TypeError('{} takes 1 positional argument but '
                            '{} were given'.format(
                                func.__name__,len(args)))
        arg = args[0]
        return curry(func,func(num,arg))
    return wrapped

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读