大家好,我是老王。
Python 開發者可能都聽說過鴨子類型和猴子補丁這兩個詞,即使沒聽過,也大概率寫過相關的代碼,只不過並不了解其背後的技術要點是這兩個字而已。
我最近在面試候選人的時候,也會問這兩個概念,很多人答的也不是很好。但是當我向他們解釋完之後,普遍都會恍然大悟:「哦,是這個啊,我用過」。
所以,我決定來寫一篇文章,探討一下這兩種技巧。
鴨子類型
引用維基百科中的一段解釋:
鴨子類型(duck typing)在程式設計中是動態類型的風格。在這種風格中,一個物件有效的語義,不是由繼承自特定的類別或實作特定的接口,而是由"當前方法和屬性的集合"決定。
更通俗一點的說:
當看到一隻鳥走起來像鴨子、游泳起來像鴨子、叫起來也像鴨子,那麼這隻鳥就可以被稱為鴨子。
也就是說,在鴨子類型中,關注點在於物件的行為,能作什麼;而不是專注於物件所屬的類型。
我們看一個例子,更形像地展示一下:
# 这是一个鸭子(Duck)类 class Duck: def eat(self): print("A duck is eating...") def walk(self): print("A duck is walking...") # 这是一个狗(Dog)类 class Dog: def eat(self): print("A dog is eating...") def walk(self): print("A dog is walking...") def animal(obj): obj.eat() obj.walk() if __name__ == '__main__': animal(Duck()) animal(Dog())
程式輸出:
A duck is eating... A duck is walking... A dog is eating... A dog is walking...
Python 是一門動態語言,沒有嚴格的類型檢查。只要 Duck 和 Dog 分別實作了 eat 和 walk 方法就可以直接呼叫。
再例如 list.extend() 方法,除了 list 之外,dict 和 tuple 也可以調用,只要它是可迭代的就都可以調用。
看過上例之後,應該對「物件的行為」和「物件所屬的類型」有更深的體會了吧。
再擴展一點,其實鴨子類型和介面挺像的,只不過沒有明確定義任何介面。
例如用 Go 語言來實現鴨子類型,程式碼是這樣的:
package main import "fmt" // 定义接口,包含 Eat 方法 type Duck interface { Eat() } // 定义 Cat 结构体,并实现 Eat 方法 type Cat struct{} func (c *Cat) Eat() { fmt.Println("cat eat") } // 定义 Dog 结构体,并实现 Eat 方法 type Dog struct{} func (d *Dog) Eat() { fmt.Println("dog eat") } func main() { var c Duck = &Cat{} c.Eat() var d Duck = &Dog{} d.Eat() s := []Duck{ &Cat{}, &Dog{}, } for _, n := range s { n.Eat() } }
透過明確定義一個 Duck 接口,每個結構體實現接口中的方法來實現。
猴子補丁
猴子補丁(Monkey Patch)的名聲不太好,因為它會在運行時動態修改模組、類別或函數,通常是新增功能或修正缺陷。
猴子補丁在記憶體中發揮作用,不會修改原始碼,因此只對目前運行的程式實例有效。
但如果濫用的話,會導致系統難以理解和維護。
主要有兩個問題:
- 補丁會破壞封裝,通常與目標緊密耦合,因此很脆弱
- 打了補丁的兩個函式庫可能相互牽絆,因為第二個庫可能會撤銷第一個庫的補丁
所以,它被視為臨時的變通方案,不是集成代碼的推薦方式。
按照慣例,還是舉個例子來說明:
# 定义一个Dog类 class Dog: def eat(self): print("A dog is eating ...") # 在类的外部给 Dog 类添加猴子补丁 def walk(self): print("A dog is walking ...") Dog.walk = walk # 调用方式与类的内部定义的属性和方法一样 dog = Dog() dog.eat() dog.walk()
程式輸出:
A dog is eating ... A dog is walking ...
這裡相當於在類別的外部為Dog 類別增加了一個walk 方法,而呼叫方式與類別的內部定義的屬性和方法一樣。
再舉一個比較實用的例子,例如我們常用的json 標準函式庫,如果說想用效能更高的ujson 取代的話,勢必需要將每個檔案的引入:
import json
改成:
import ujson as json
如果這樣改起來成本就比較高了。這個時候就可以考慮使用猴子補丁,只需要在程式入口加上:
import json import ujson def monkey_patch_json(): json.__name__ = 'ujson' json.dumps = ujson.dumps json.loads = ujson.loads monkey_patch_json()
這樣在以後呼叫 dumps 和 loads 方法的時候就是呼叫的 ujson 包,還是很方便的。
但猴子補丁就是一把雙面刃,問題也在上文提到了,看需,謹慎使用吧。
以上是Python 中的鴨子類型和猴子補丁的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具