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

Python的Mixin類學習

系統 1721 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條評論
主站蜘蛛池模板: 北辰区| 中宁县| 彭阳县| 蚌埠市| 鄂托克旗| 手机| 什邡市| 桃源县| 得荣县| 威宁| 山阳县| 井研县| 江安县| 沽源县| 肃北| 靖西县| 孝义市| 南木林县| 炎陵县| 九台市| 留坝县| 许昌县| 大厂| 呼伦贝尔市| 巴马| 漠河县| 武胜县| 田阳县| 洪雅县| 江达县| 兴海县| 获嘉县| 汶川县| 泸州市| 比如县| 赤壁市| 太康县| 喀喇沁旗| 天镇县| 观塘区| 临夏县|