什么是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

Mixins and Python的更多相关文章

  1. Python资源汇总

    Python 目录: 管理面板 算法和设计模式 反垃圾邮件 资产管理 音频 验证 构建工具 缓存 ChatOps工具 CMS 代码分析和Linter 命令行工具 兼容性 计算机视觉 并发和并行性 组态 ...

  2. Python 学习教程汇总

    Python快速教程http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html简明Python教程https://bop.molun.ne ...

  3. Life is short.,You need Python

    真棒Python  https://awesome-python.com/ 精选的Python框架,库,软件和资源的精选列表. 灵感来自awesome-php. 真棒Python 管理员面板 算法和设 ...

  4. (转)Python Mixins 机制

    原文:https://github.com/dengshuan/notes/blob/master/techs/python-mixins.org https://blog.csdn.net/u012 ...

  5. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  6. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  7. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  8. [Python][flask][flask-login]关于flask-login中各种API使用实例

    本篇博文跟上一篇[Python][flask][flask-wtf]关于flask-wtf中API使用实例教程有莫大的关系. 简介:Flask-Login 为 Flask 提供了用户会话管理.它处理了 ...

  9. Python 经典面试题汇总之框架篇

    前端和框架 1.谈谈你对http协议的认识 浏览器本质,socket客户端遵循Http协议 HTTP协议本质:通过\r\n分割的规范,请求响应之后断开链接 ==> 短连接.无状态 具体: Htt ...

随机推荐

  1. GO基础之函数的高级用法

    一.可变参数 支持可变长参数列表的函数可以支持任意个传入参数,比如fmt.Println函数就是一个支持可变长参数列表的函数. package main import "fmt" ...

  2. .Net Core MVC中过滤器简介

    在.Net Framework MVC 中有四种过滤器,授权过滤器(Authorize).Action 过滤器.结果过滤器(Result).异常过滤器(Exception)四种过滤器.在.Net Co ...

  3. 机器学习pipeline总结

    # -*- coding: utf-8 -*- """scikit-learn introduction Automatically generated by Colab ...

  4. 【Gradle】Android Gradle 多项目构建

    Android Gradle 多项目构建 Android 项目区别 Android项目一般分为库项目,应用项目,测试项目,Android Gradle 根据这些项目分别对应3种插件:com.andro ...

  5. Linux相关集合

    本篇概述 Linux xshell6 连接 Hadoop 启动关闭 Linux xshell6 连接相关问题 首先,虚拟机 得先能通成网(具体教程可百度) 然后,进行 本机 ip 的查询(xshell ...

  6. WORD 和Uint16的区别

    UINT   A 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-bit unsigned integer on Win32 ...

  7. 使用Anaconda3的Docker镜像

    假设本地 Ubuntu 服务器已经安装好了Docker,这里讲述一下如何开始运行Anaconda3的Docker镜像: 1. 搜索镜像 搜索我们想要的anaconda镜像: docker search ...

  8. Linux-3.14.12内存管理笔记【建立内核页表(3)

    前面已经分析了内核页表的准备工作以及内核低端内存页表的建立,接着回到init_mem_mapping()中,低端内存页表建立后紧随着还有一个函数early_ioremap_page_table_ran ...

  9. YUM命令总结

    1.关于YUM源 Yum 全称为 Yellow dog Updater Modified,它是一个在线的软件安装命令. 能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装 ...

  10. coredump配置、产生、分析以及分析示例

    关键词:coredump.core_pattern.coredump_filter等等. 应用程序在运行过程中由于各种异常或者bug导致退出,在满足一定条件下产生一个core文件. 通常core文件包 ...