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 ...
随机推荐
- 记录一次netcore3.0 code first使用迁移命令报错问题
环境描述:macOS .vscode .netcore3.0 迁移工具:Microsoft.EntityFrameworkCore.Tools 遇到的问题: Could not execute bec ...
- 百度地图API----搜索地址,获取该点的位置坐标并转换成WebMercator
function doFind(){ LoadBaiduMapScript().then(BaiduMap).then(function () { //查询并获取坐标 var myGeo = new ...
- 【转载】Android IntentService使用全面介绍及源码解析
一 IntentService介绍 IntentService定义的三个基本点:是什么?怎么用?如何work? 官方解释如下: //IntentService定义的三个基本点:是什么?怎么用?如何wo ...
- udp协议,进程(同步,异步)
udp协议与进程 一.udp协议 QQ聊天室 #- sever import socket #socket.SOCK_DGRAM--->UPD协议 sever = socket.socket(t ...
- Linux日志中出现大量dhclient mesage浅析
最近检查发现一台Linux服务器,发现其日志里面有大量下面信息,其中部分信息做了脱敏处理.其中一个地址A(192.168.AAA.AAA) 为DNS服务器地址,地址B(192.168.BBB.BBB) ...
- Linux-3.14.12内存管理笔记【构建内存管理框架(1)】
传统的计算机结构中,整个物理内存都是一条线上的,CPU访问整个内存空间所需要的时间都是相同的.这种内存结构被称之为UMA(Uniform Memory Architecture,一致存储结构).但是随 ...
- Linux Ctrl + Alt + Fx | (x = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
VMware Ubuntu中,按下 Ctrl + Alt + Fx | (x = 1...12),会出现不同的效果. 1. Ctrl + Alt + F1 ~ F6 Ctrl + Alt + F1 ~ ...
- Windows 10 任务栏添加网易云音乐控制按钮
软件背景: 算是老帖新发,之前有朋友分享过一个很好用的工具,但可能是因为网易云软件更新后,导致控件失灵了,只剩下歌词控件有用了,所以今天用python重新写了一个小工具,发出来分享给大家,附上之前 ...
- GO语言规范
1.golang的命名推荐使用驼峰命名法,必须以一个字母(Unicode字母)或下划线开头,后面可以跟任意数量的字母.数字或下划线. 2.golang中根据首字母的大小写来确定可以访问的权限.无论是方 ...
- 201871010113-刘兴瑞《面向对象程序设计(java)》第十一周学习总结
项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址>htt ...