Python中物件方法的定義很怪異,第一個參數一般都命名為self(相當於其它語言的this),用於傳遞物件本身,而在呼叫的時候則不必明確傳遞,系統會自動傳遞。
今天我們介紹的主角是super(), 在類別的繼承裡面super()非常常用, 它解決了子類別呼叫父類別方法的一些問題, 父類別多次被呼叫時只執行一次, 優化了執行邏輯,下面我們就來詳細看一下。
舉一個例子:
class Foo: def bar(self, message): print(message)
>>> Foo().bar("Hello, Python.") Hello, Python.
當存在繼承關係的時候,有時候需要簡單的方法中轉換父嗎成類調用,要注意的是這時self參數需要明確傳遞,例如:
class FooParent: def bar(self, message): print(message) class FooChild(FooParent): def bar(self, message): FooParent.bar(self, message)
>>> FooChild().bar("Hello, Python.") Hello, Python.
class FooParent: def bar(self, message): print(message) class FooChild(FooParent): def bar(self, message): super(FooChild, self).bar(message)
>>> FooChild().bar("Hello, Python.") Hello, Python.bar(self, message)方法的結果是一致的,實際上這兩種方法的內部處理機制大大不同,當涉及多繼承情況時,就會表現出明顯的差異來,直接給例子:
class A: def __init__(self): print("Enter A") print("Leave A") class B(A): def __init__(self): print("Enter B") A.__init__(self) print("Leave B") class C(A): def __init__(self): print("Enter C") A.__init__(self) print("Leave C") class D(A): def __init__(self): print("Enter D") A.__init__(self) print("Leave D") class E(B, C, D): def __init__(self): print("Enter E") B.__init__(self) C.__init__(self) D.__init__(self) print("Leave E") E()
Enter C
Enter A
Leave A
Leave C
Enter D
Enter A
Leave A
Leave D
Leave E
。代碼二:
class A: def __init__(self): print("Enter A") print("Leave A") class B(A): def __init__(self): print("Enter B") super(B, self).__init__() print("Leave B") class C(A): def __init__(self): print("Enter C") super(C, self).__init__() print("Leave C") class D(A): def __init__(self): print("Enter D") super(D, self).__init__() print("Leave D") class E(B, C, D): def __init__(self): print("Enter E") super(E, self).__init__() print("Leave E") E()
Leave C
Leave B
Leave E
在 Resuper機制裡可以保證公共父類僅被執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類僅被執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類別僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類別僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類別僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類別僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡可以確保公共父類別僅執行一次,至於執行的順序,是按照MRO(Method Resuper機制裡):方法解析到執行的順序的。後續會詳細介紹一下這個MRO機制。

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 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境

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

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

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