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
Mixins and Python的更多相关文章
- Python资源汇总
Python 目录: 管理面板 算法和设计模式 反垃圾邮件 资产管理 音频 验证 构建工具 缓存 ChatOps工具 CMS 代码分析和Linter 命令行工具 兼容性 计算机视觉 并发和并行性 组态 ...
- Python 学习教程汇总
Python快速教程http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html简明Python教程https://bop.molun.ne ...
- Life is short.,You need Python
真棒Python https://awesome-python.com/ 精选的Python框架,库,软件和资源的精选列表. 灵感来自awesome-php. 真棒Python 管理员面板 算法和设 ...
- (转)Python Mixins 机制
原文:https://github.com/dengshuan/notes/blob/master/techs/python-mixins.org https://blog.csdn.net/u012 ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
- [Python][flask][flask-login]关于flask-login中各种API使用实例
本篇博文跟上一篇[Python][flask][flask-wtf]关于flask-wtf中API使用实例教程有莫大的关系. 简介:Flask-Login 为 Flask 提供了用户会话管理.它处理了 ...
- Python 经典面试题汇总之框架篇
前端和框架 1.谈谈你对http协议的认识 浏览器本质,socket客户端遵循Http协议 HTTP协议本质:通过\r\n分割的规范,请求响应之后断开链接 ==> 短连接.无状态 具体: Htt ...
随机推荐
- SSM框架之Mybatis(7)延迟加载、缓存及注解
Mybatis(7)延迟加载.缓存及注解 1.延迟加载 延迟加载: 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据.延迟加载也称懒加载. **好处:**先从单表查询,需要时再从关联表去关 ...
- Cesium专栏-空间分析之剖面分析(附源码下载)
Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...
- MAC本地生成SSH KEY的方法
由于时间原因,直接转载,后期有空再来好好整理一下,大家先凑合着用哈: 参考链接:https://blog.csdn.net/wangjunling888/article/details/5111565 ...
- P1005 Spell It Right
# P1005 Spell It Right 原题 Given a non-negative integer N, your task is to compute the sum of all the ...
- 【Java基础】Java中的反射机制
一.反射的理解 (1)正射 在理解反射这个概念之前,我们先来理解Java中的“正射”. 我们在编写代码时,当需要使用到某一个类的时候,必定先会去了解这是一个什么类,是用来做什么的,有怎么样的功能. 之 ...
- 036.[转] JNDI 学习
使用外置服务器(如tomcat)时,如果一个服务器启动多个项目,可以使用JNDI配置数据源,这样每个项目都可以获取到Tomcat 配置的 JNDI的数据源. 在学习 jsp 的时候,作用域对象 pag ...
- 读数笔记_python网络编程3(4)
4.套接字名与DNS 讨论网络地址,描述将主机名解析为原始IP地址的分布式服务 4.1. 主机名与socket 浏览器汇总一般键入域名.有些域名标识整个机构.如,python.org,而另一些指定了主 ...
- 05-Django后台管理和视图
Django的后台管理可以方便的生成管理页面,使用前先准备如下: 1.本地化 语言和时区的本地化,修改settings.py文件 # LANGUAGE_CODE = 'en-us' LANGUAGE_ ...
- [Linux]终端设备关系
1.概述 tty中基本上可以划分为console(/dev/console).虚拟终端(/dev/tty0~n)和伪终端(/dev/pts/0~n). 它们之间存在一定的关系. 为了说明这段关系,先要 ...
- 关于scanf的一些知识
10.22,对现阶段已知道的scanf的一些用法或注意事项的一些总结: 1.scanf中,赋值的那个数据前面一定加&! 2.若情景要求必须输入空格的,scanf("%d%c%d&qu ...