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函数实例化

python函数实例化_用Python实例化函数相关推荐

  1. python命名规则数字开头的成语_浅谈Python中带_的变量或函数命名

    搜索热词 Python 的代码风格由 PEP 8 描述.这个文档描述了 Python 编程风格的方方面面.在遵守这个文档的条件下,不同程序员编写的 Python 代码可以保持最大程度的相似风格.这样就 ...

  2. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  3. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  4. 理解python的类实例化_理解python的类实例化

    让我们以一个Foo类开始: class Foo(object): def __init__(self, x, y=0): self.x = x self.y = y 当你实例化它(即创建该类的一个新的 ...

  5. turtle 函数 方法_学python第十一节:turtle深入 了解

    学python第十一节:深入分析turtle Turtle是一个直观有趣的图形绘制函数. 这节课对turtle的以下几点进行补充: 在蟒蛇绘制代码中提到过import 库引用保留字的函数,是补充pyt ...

  6. python常用函数中文_【python】python常用函数

    urlencode与urldecode 当url中包含中文或者参数包含中文,需要对中文或者特殊字符(/.&)做编码转换. urlencode的本质:把字符串转为gbk编码,再把\x替换成%.如 ...

  7. python函数复用_【python学习-4】可复用函数与模块

    1.自定义函数 自定义函数格式如下: def (参数列表):return #!/usr/bin/python#定义函数,打印数字1~5 defprintNum5():#range函数,生成列表集合,有 ...

  8. python pow和**_「Python学习笔记」Python函数高级应用

    Python, 函数本身也是一个对象函数既可以赋值,也可以用作其他函数的参数,还可作为其他函数的返回值. 使用函数变量 Python的函数也是一种值:所有函数都是function对象,这意味着可以把函 ...

  9. python函数示例_使用Python中的示例的input()函数

    python函数示例 Python input()函数 (Python input() function) input() function is a library function, it is ...

  10. python map用法_讲解Python map()函数和将map()与多个Iterables一起使用

    map()是Python中的内置函数,可将函数应用于给定可迭代对象中的所有元素,它使您无需使用循环即可编写简单干净的代码. Python map()函数 map()函数采用以下形式: map(func ...

最新文章

  1. cocos游戏源码怎么用_亲子游戏怎么玩?游戏方式用对了,才会事半功倍
  2. c:if判断参数是否为空
  3. 关于单位基金资产净值
  4. .Net线程同步技术解读
  5. [渝粤教育] 广东-国家-开放大学21秋期末考试中国近现代史纲要(A)10881k1
  6. OpenCV windows 上安装
  7. RHEL 8 - 安装 webconsole
  8. 浅谈C#的垃圾回收-关于GC、析构函数、Dispose、and Finalize asp.net GC之析构函数详解...
  9. Photoshop cc2015基础课笔记
  10. Json转换为Model,Struct,Class对象 Swift
  11. 一卡通管理系统总体设计
  12. 计算机导师问读研计划和后续计划,考研面试,问“研究生时期的规划”怎么回答急...
  13. 理解蓝绿发布、灰度发布和滚动发布
  14. 好123主页篡改修复方法
  15. 【mininet 0x02】如何使用mn工具来操作mininet
  16. VUE div click无效
  17. 学习笔记16 window service 服务的相关内容
  18. C语言 “吞字符” 加不加‘\n‘ ---- #145. 最长的名字
  19. unity2d游戏开发系列教程:一、环境安装
  20. 2016年3月23日日本本州岛之旅

热门文章

  1. html溢出左右滚动,html-选项卡溢出时水平滚动
  2. python自定义模块和三方模块_python基础知识8——模块1——自定义模块和第三方开源模块...
  3. Docker 网络命名空间
  4. 获取Linux内存、cpu、磁盘IO等信息
  5. 【原创】利用腾讯和百度的AI接口识别验证码
  6. SpringMVC注解HelloWorld
  7. 发现保存GIF格式后相素发生变化咋办
  8. Feature selection
  9. C# 导出word文档及批量导出word文档(3)
  10. ORA-00304: requested INSTANCE_NUMBER is busy