什么是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. ABP入门教程14 - 更新多语言

    点这里进入ABP入门教程目录 设置语种 新增语种 数据库操作 打开多语言表AbpLanguages,添加一条记录. 程序操作 在基础设施层(即JD.CRS.EntityFrameworkCore)的\ ...

  2. 20180918 begin

    20180918-20190717 风 雅 颂(305,每天一首): 诗经鉴赏, 180918-1030 魔鬼经济学 <唐宋词十七讲>叶嘉莹<最美的宋词> 布谷鸟<诗境浅 ...

  3. Saltstack_使用指南16_syndic

    1. 主机规划 salt 版本 [root@salt100 ~]# salt --version salt (Oxygen) [root@salt100 ~]# salt-minion --versi ...

  4. 微信扫码登陆,qq登陆,微博登陆等第三方登陆成功后返回原来的页面并进行跳转

    原理很简单,主要是利用到window.open的第二个属性,name 前端: 原来的网页给window命名为 window.name="single" window.open(“第 ...

  5. mssql sqlserver 如何编写case when 多条件呢?

    摘要: 下文讲述case when中多条件的编写方法,如下所示: 实验环境:sql server 2008 R2  case when 多条件编写方法  case when多条件编写语法: case ...

  6. 多个div使用display:inline-block时候div之间有间隔

    开发场景中用到display:inline-block;然后呢,div间就有间隙,但是ajax加载出来的数据没有间隙,解决办法如下   display:inline-block表示行内块元素,后面自带 ...

  7. ubuntu 18.04下安装JDK

    一.安装前检查 检查是否已经安装 java -version 二.安装方式 1)通过ppa(源) 2)通过官网安装包安装  JDK官网下载地址  或百度云下载地址,提取码 rzq5 三.安装步骤 (一 ...

  8. Shadow Map -- 点阴影(全方位)

    昨晚终于把点阴影(深度CubeMap)程序调通了,思想不难,基本就是在上节定向光阴影基础上稍作修改,但是CG程序不太方便Debug,需要输出中间效果图进行判断,耽搁了一会儿. 过程如下: 1.将深度渲 ...

  9. MVC(基础一)

    MVC学习之前必须掌握的c#基础知识 一.类自动属性 public class Person { //自动属性 public string Name { get; set; } private int ...

  10. 给用户提供就医帮助的安卓APP

    经过我们的小组的成员讨论,我们确定了我们小组的项目,即是一款给用户提供就医帮助的安卓APP. 项目计划及功能:计划两个月内团队成员共同开发完成此款APP,此款APP提供预约挂号,名医名院咨询, 就医导 ...