在Python中,將另一個函數作為參數或將函數作為輸出傳回的函數稱為高階函數。讓我們來看看其特性 -
該函數可以儲存在變數中。
此函數可以當作參數傳遞給另一個函數。
高階函數可以以列表、雜湊表等形式儲存
#函數可以從函數中傳回。
讓我們來看一些例子 −
函數作為物件
Example
的中文翻譯為:範例
在這個例子中,這些函數被視為物件。在這裡,函數demo()被賦值給一個變數 -
# Creating a function def demo(mystr): return mystr.swapcase() # swapping the case print(demo('Thisisit!')) sample = demo print(sample('Hello'))
輸出
tHISISIT! hELLO
將函數傳遞為參數
Example
的中文翻譯為:範例
在此函數作為參數傳遞。 demo3() 函數呼叫 demo() 和 demo2() 函數作為參數。
def demo(text): return text.swapcase() def demo2(text): return text.capitalize() def demo3(func): res = func("This is it!") # Function passed as an argument print (res) # Calling demo3(demo) demo3(demo2)
輸出
tHIS IS IT! This is it!
現在,讓我們討論裝飾器。我們可以使用裝飾器作為高階函數。
Python中的裝飾器
Example
的中文翻譯為:範例
在裝飾器中,函數被當作參數傳遞給另一個函數,然後在包裝函數中被呼叫。讓我們來看一個快速的例子 −
@mydecorator def hello_decorator(): print("This is sample text.")
上面也可以寫成 -
def demo_decorator(): print("This is sample text.") hello_decorator = mydecorator (demo_decorator)
裝飾器範例
Example
的中文翻譯為:範例
在這個例子中,我們將把裝飾器當作高階函數來運作 -
def demoFunc(x,y): print("Sum = ",x+y) # outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc # calling demoFunc2 = outerFunc(demoFunc) demoFunc2(10, 20)
輸出
Sum = 30
Example
的中文翻譯為:範例
def demoFunc(x,y): print("Sum = ",x+y) # outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc # calling demoFunc2 = outerFunc(demoFunc) demoFunc2(10, 20)
輸出
Sum = 30
應用語法裝飾器
Example
的中文翻譯為:範例
可以使用帶有 @symbol 的裝飾器來簡化上面的範例。透過在我們想要裝飾的函數之前放置 @ 符號,可以簡化裝飾器的應用 -
# outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc @outerFunc def demoFunc(x,y): print("Sum = ",x+y) demoFunc(10,20)
輸出
Sum = 30
以上是如何在Python中創建高階函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

WebStorm Mac版
好用的JavaScript開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具