惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
SegmentFault 最新的问题
Last Week in AI
Last Week in AI
阮一峰的网络日志
阮一峰的网络日志
Cloudbric
Cloudbric
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Security @ Cisco Blogs
月光博客
月光博客
T
Troy Hunt's Blog
H
Help Net Security
Forbes - Security
Forbes - Security
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
L
LINUX DO - 最新话题
Hacker News - Newest:
Hacker News - Newest: "LLM"
GbyAI
GbyAI
S
Schneier on Security
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
Blog — PlanetScale
Blog — PlanetScale
N
News | PayPal Newsroom
F
Fortinet All Blogs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
博客园_首页
Martin Fowler
Martin Fowler
Stack Overflow Blog
Stack Overflow Blog
雷峰网
雷峰网
O
OpenAI News
I
Intezer
S
Security Affairs
罗磊的独立博客
T
Tailwind CSS Blog
小众软件
小众软件
P
Palo Alto Networks Blog
Help Net Security
Help Net Security
V
Vulnerabilities – Threatpost
博客园 - 【当耐特】
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
Lohrmann on Cybersecurity
C
Cisco Blogs
Security Latest
Security Latest

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
Mixin技术学习笔记
2014-09-03 · via 夜行人

折腾edX和Django Rest Framework的时候都看到Mixin的身影,决定弄清楚它。

##解释 Mixin即mix in,就是混入的意思。在不改变原对象的情况下对其进行扩展. 在运行期间,动态改变类的基类或类的方法
利用Mixin我们还可以增加无数总特征,而无需对已有代码作太大改动。实现无痛扩展

多重继承类似.

但通常主类和 Mixin 类本身不是 is-a 的关系,混入 Mixin 类是为了添加某些(可选的)功能。自由地混入 Mixin 类就可以灵活地为被混入的类添加不同的功能。

传统的「接口」概念中并不包含实现,而 Mixin 包含实现。Mixin 提供了(默认)实现

wu:是为了额外获得方法?

Mixin的好处是可以为主类添加任意多的Mixin来实现多态

##QuickStart

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Fruit(object):
       pass
class GiftMixin(object):
       def is_gift_fruit(self):
              return True
class NotGiftMixin(object):
       def is_gift_fruit(self):
              return False
class PareMixin(object):
       def eat_method(self):
              return 'Pare'
class HuskMixin(object):
       def eat_method(self):
              return 'Husk'
class Apple(GiftMixin, PareMixin, Fruit):pass
class Orange(GiftMixin, HuskMixin, Fruit):pass
class Pear(NotGiftMixin, PareMixin, Fruit):pass
class Banana(NotGiftMixin, HuskMixin, Fruit):pass

这样水果就知道自己的种类和特性啦

##何时用 There are two main situations where mixins are used:

  • You want to provide a lot of optional features for a class.
  • You want to use one particular feature in a lot of different classes.

摘自what-is-a-mixin-and-why-are-they-useful这篇回答,就是说当一个功能特性被很多个类用到时,或者一个类需要很多功能特性时

##demo ###werkzeug中Request / Response 答主随后举了werkzeug中Request / Response的例子

If I wanted to make a request object that supports accept headers, etags, authentication, and user agent support, I could do this:

1
2
3
4
from werkzeug import BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthorizationMixin

class Request(BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthorizationMixin):
    pass

###django-rest-framework.org/tutorial/3 许多公共行为在REST framework’s mixin类中都实现了.方便的组合与重用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework import mixins
from rest_framework import generics

class SnippetList(mixins.ListModelMixin,
                   mixins.CreateModelMixin,
                   generics.MultipleObjectAPIView):
     model = Snippet
     serializer_class = SnippetSerializer
 
     def get(self, request, *args, **kwargs):
         return self.list(request, *args, **kwargs)
 
     def post(self, request, *args, **kwargs):
         return self.create(request, *args, **kwargs)

MultipleObjectAPIView构建了view, 然后加上了inListModelMixin和CreateModelMixin.

基类提供了核心功能,mixin类提供了 .list() 和 .create() 动作。然后显式的把 get 和 post 方法与合适的动作绑定在一起。

##参考资料

文章作者 种瓜

上次更新 2014-09-03