python函数实例化_用Python实例化函数

本文介绍了柯里化在数学和计算机科学中的概念,它是将多参数函数转换为一系列单参数函数的技术。通过示例代码展示了如何在Python中实现柯里化,将复杂函数简化为易于执行的单参数函数,从而提高编程效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python函数实例化

In terms of Mathematics and Computer science, currying is the approach/technique by which we can break multiple-argument function to single argument function.

从数学和计算机科学的角度来看, 柯里化是一种方法/技术,通过它我们可以将多参数功能分解为单参数功能。

Mathematical illustration: h(x)=g(f(x))

数学图示:h(x)= g(f(x))

The composition of two functions is a chaining process in which the output becomes the input for the outer function.

两个函数的组合是一个链接过程,其中输出成为外部函数的输入。

    def currying( g , f ):
        def h(x):
            return g(f(x))
        return h

In the technical term "Currying is the process of transforming a function that takes 'n' arguments into a series of 'n' function that only takes the one argument each."

在技​​术术语中, “固化是将采用'n'个参数的函数转换为一系列只采用一个参数的'n'函数的过程。”

In problem-solving approach, currying is to be done to simplify the programming i.e. execution of the function which takes multiple arguments into the single - single argument functions.

在解决问题的方式, 钻营被做了简化这需要多个参数到单一功能的编程也就是执行-单参数的函数。

Example code 1:

示例代码1:

def f(a):
    def g(b, c, d, e):
        print(a, b, c, d, e)
    return g #as in f it return g this is currying
 
f1 = f(1)

f1(2,3,4,5)

Output

输出量

1 2 3 4 5

Explanation of the above code:

上面的代码说明:

Now, f(1) returns the function g(b, c, d, e) which, for all b, c, d, and e, behaves exactly like f(1, b, c, d, e). Since g is a function, it should support currying as well.

现在, f(1)返回函数g(b,c,d,e) ,对于所有b , c , d和e ,其行为都与f(1,b,c,d,e)完全一样。 由于g是一个函数,因此它也应该支持curring

Example code 2:

示例代码2:

def f(a):
    def g(b):
        def h(c):
            def i(d):
                def j(e):
                    print(a, b, c, d, e)
                return j  #return to function i
            return i      #return to function h
        return h          #return to function g
    return g              #return to function f
 
f(1)(2)(3)(4)(5)

Output

输出量

1 2 3 4 5

Explanation of the above code:

上面的代码说明:

In the code we have, X(a,b,c,d,e) = f(g(h(i(j(a,b,c,d,e))))

在代码中, X(a,b,c,d,e)= f(g(h(i(j(a,b,c,d,e))))

Here, the concept is of nesting of one function to another and hence the result of one function get stored or recorded in another function as a chain of functions.

在此,概念是将一个功能嵌套到另一个功能,因此一个功能的结果作为功能链存储或记录在另一个功能中。

Note: Now f(a,b,c,d,e) is no more i.e. now f no more take 5 arguments.

注意:现在f(a,b,c,d,e)不再存在,即现在f不再接受5个参数。

Python example of currying function to convert INR to pounds

将INR转换为英镑的currying函数的Python示例

# program to covert the Indian Rupee to Pound
# Demonstrating  Currying of composition of function
def change(b, c): 
    def a(x): #by currying function
        return b(c(x)) 
    return a 
      
def IR2USD(ammount):  
    return ammount*0.014  # as 1IR=0.014USD
      
def USD2Pound(ammount):  # Function converting USD to Pounds 
    return ammount * 0.77 
      
if __name__ == '__main__': 
    Convert = change(IR2USD,USD2Pound )
    
    e = Convert(565)
    print("Conveted Indian Rupee to Pound:")
    print('565 INDIAN RUPEES =',e,'PoundSterling')      

Output

输出量

Conveted Indian Rupee to Pound:
565 INDIAN RUPEES = 6.0907 PoundSterling


翻译自: https://www.includehelp.com/python/currying-function-with-example.aspx

python函数实例化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值