zoukankan      html  css  js  c++  java
  • Mixins and Python

    什么是Mixin (混入)

    Mixin 这个词在Python/Ruby中经常使用, Java 中几乎看不到这个名词.

    在Java 中, 我们经常定一个一个子类扩展了某个基类, 同时实现某些接口. 因为 Python 不支持接口, 但支持多重继承, 为了实现Java的这个功能, 在Python中, 不得不使用多重继承语法, 但我们直到多重继承往往会引入很多问题, 为了规避多重继承问题, Python/Ruby社区使用的 mixin 模式, 写法如下:

    class MyClass(Mixin2, Mixin1, BaseClass):
        pass

    Mixin2 和 Mixin1 可以等同于Java 中的接口, Java 的接口往往仅仅定义一个规范, 需要在实现类中对接口函数做实现.  Python 中的 Mixin 类往往已经带上了实现, 在子类中一般不做实现, 同时 Mixin 类名应该包含 Mixin ziy字样, 以表明它不是普通的类, 仅仅是一个接口, 就像是 Java 中经常使用 able 作为 interface 的后缀一样. 

    Mixin 类 和  BaseClass 书写的顺序

    https://www.ianlewis.org/en/mixins-and-python

    Python supports a simple type of multiple inheritance which allows the creation of Mixins. Mixins are a sort of class that is used to "mix in" extra properties and methods into a class. This allows you to create classes in a compositional style.

    Mixins are a really great concept but I often find that people use them incorrectly which can lead to some bugs. I often see Mixins used like the following:

    class Mixin1(object):
        def test(self):
            print "Mixin1"
    
    class Mixin2(object):
        def test(self):
            print "Mixin2"
    
    class MyClass(BaseClass, Mixin1, Mixin2):
        pass
    

    However, in Python the class hierarchy is defined right to left, so in this case the Mixin2 class is the base class, extended by Mixin1and finally by BaseClass. This is usually fine because many times the mixin classes don't override each other's, or the base class' methods. But if you do override methods or properties in your mixins this can lead to unexpected results because the priority of how methods are resolved is from left to right.

    >>> obj = MyClass()
    >>> obj.test()
    Mixin1
    

    The correct way to use mixins is like in the reverse order:

    class MyClass(Mixin2, Mixin1, BaseClass):
        pass
    

    This kind of looks counter-intuitive at first because most people would read a top-down class hierarchy from left to right but if you include the class you are defining, you can read correctly up the class hierarchy (MyClass => Mixin2 => Mixin1 => BaseClass. If you define your classes this way you won't have to many conflicts and run into too many bugs.

    >>> obj = MyClass()
    >>> obj.test()
    Mixin2
  • 相关阅读:
    2020学习 04 python 爬取某政府网站信件
    2020学习03
    2020学习02
    阅读笔记--《一线架构师实践指南》--Pre-architecture阶段
    重构“信息领域热词分析”,实现六种质量属性战术
    pycharm错误:Error updating package list: connect timed out解决
    python连接mysql数据库——编码问题
    阅读笔记--《大型网站技术架构》—02架构
    python查询MySQL数据库的表以及所有字段
    python连接mysql数据库——版本问题
  • 原文地址:https://www.cnblogs.com/harrychinese/p/python_mixin.html
Copyright © 2011-2022 走看看