日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python的Mixin類學習

系統 2175 0

目錄

Mixin 類

Mixin 類的實例

運行流程

流程圖

《Python GUI Programming with Tkinter》作者的話


Mixin 類

Mixin 類只包含了一組特定的函數集合,而我們將會將其與其他類進行混合,從而生成一個適用于實際需要的新類

Mixin 類的實例

代碼改編自《Python GUI Programming with Tkinter》

            
              class Displayer():
    def display(self, message):
        print('2:display:Displayer')
        print(message)

class LoggerMixin():
    def log(self, message, filename='logfile.txt'):
        print('5:log:LoggerMixin')
        with open(filename, 'a') as fh:
            fh.write(message)

    def display(self, message):
        print('1:display:LoggerMixin')
        super().display(message)
        print('3:display:LoggerMixin')
        self.log(message)

class MySubClass(LoggerMixin, Displayer):
    def log(self, message):
        print('4:log:MySubClass')
        super().log(message, filename='subclasslog.txt')

subclass = MySubClass()
subclass.display("此字符串將顯示并記錄在subclasslog.txt中.")

            
          
            
              E:\DataAnalysis\tools\python3\python.exe E:/DataAnalysis/tools/python3/project/money_analysis/text/object_test.py
1:display:LoggerMixin
2:display:Displayer
此字符串將顯示并記錄在subclasslog.txt中.
3:display:LoggerMixin
4:log:MySubClass
5:log:LoggerMixin

Process finished with exit code 0

            
          

運行流程

Python的Mixin類學習_第1張圖片

            
                  '''In a multiple inheritance situation, super() does something a little more complex
than just standing in for the superclass. It look up the chain of inheritance using
 something called the Method Resolution Order and determines the nearest class that defines
 the method we’re calling.

    在多繼承的環境下,super() 有相對來說更加復雜的含義。它會查看你的繼承鏈,使用一種叫做 Methods
 Resolution Order(方法解析順序) 的方式,來決定調用最近的繼承父類的方法。'''
            
          

在我們調用 MySubClass.display() ,觸發行為如下:

1.MySubClass.display() 方法被解析為 LoggerMixin.display() 方法的調用

對于 MySubClass 類來說,在繼承鏈上有兩個父類, LoggerMixin 最近,因此調用其方法。

2. LoggerMixin.display() 方法調用了 super().display()

在繼承鏈上,是逐個繼承的,后者是前者父類,查看運行結果,調用 Displayer 類的 display() 方法。

3.? LoggerMixin 類還調用了 self.log() 方法。

盡管self在LoggerMixin 語境中,但是由MySubClass 繼承并調用,所以self指向MySubClass 實例,執行了 MySubClass.log()

MySubClass.log() 執行 super().log() 方法,根據繼承鏈尋找最近父類,執行了 LoggerMixin.log()

流程圖

?

《Python GUI Programming with Tkinter》作者的話

            
                 ''' If this seems confusing, just remember that self.method() will look for method() in
 the current class first, then follow the list of inherited classes from left to right 
until the method is found. The super().method() will do the same, except that it skips the 
current class.

如果這看起來很混亂,請記住self.method()將首先在當前類中查找method(),然后從左到右跟隨繼承類的
列表,直到找到該方法。super().method()將執行相同的操作,只不過它跳過了當前類。  '''
            
          

為什么要叫做 Mixin 類呢?

            
                  '''Note that LoggerMixin is not usable on its own: it only works when combined with a 
class that has a display() method. This is why it’s a mixin class because it’s meant to be 
mixed in to enhance other classes.

也就是說,我們的 LoggerMixin 類是無法單獨使用的,它必須要和一個擁有 display() 函數定義的類一起混合
使用。這也就是為什么它被稱作是 Mixin 類的原因,它總是需要與其他類混合來加強其他類。'''
            
          

?


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 克什克腾旗| 洪洞县| 灌南县| 金山区| 邻水| 石屏县| 永平县| 武定县| 临颍县| 宿州市| 鄂托克旗| 额尔古纳市| 永济市| 莫力| 南华县| 栾城县| 哈密市| 怀化市| 东至县| 琼中| 旌德县| 汕头市| 莎车县| 泰和县| 巫溪县| 呼和浩特市| 秦皇岛市| 陇川县| 大渡口区| 若羌县| 大埔区| 象山县| 枣阳市| 化州市| 武功县| 石渠县| 泗水县| 江山市| 静海县| 临沧市| 桃江县|