python函数实例化_用Python实例化函数
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 hIn 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 curryingf1 = f(1)f1(2,3,4,5)Output
輸出量
1 2 3 4 5Explanation 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 ireturn i #return to function hreturn h #return to function greturn g #return to function ff(1)(2)(3)(4)(5)Output
輸出量
1 2 3 4 5Explanation 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 functionreturn b(c(x)) return a def IR2USD(ammount): return ammount*0.014 # as 1IR=0.014USDdef 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实例化函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pipedreader_Java Pip
- 下一篇: ruby array_Ruby中带有示例