在Python编程中,Mark Lutz提到了术语mixin。我有C/ c++ / c#的背景,以前没有听说过这个术语。什么是mixin?
从这个例子的字里行间(我链接了这个例子,因为它很长),我假设这是一个使用多重继承来扩展类的情况,而不是适当的子类化。这样对吗?
为什么我要这样做,而不是把新功能放到一个子类?就此而言,为什么混合/多重继承方法会比使用组合更好呢?
mixin与多重继承的区别是什么?这只是语义上的问题吗?
在Python编程中,Mark Lutz提到了术语mixin。我有C/ c++ / c#的背景,以前没有听说过这个术语。什么是mixin?
从这个例子的字里行间(我链接了这个例子,因为它很长),我假设这是一个使用多重继承来扩展类的情况,而不是适当的子类化。这样对吗?
为什么我要这样做,而不是把新功能放到一个子类?就此而言,为什么混合/多重继承方法会比使用组合更好呢?
mixin与多重继承的区别是什么?这只是语义上的问题吗?
当前回答
这不是一个Python的例子,但在D编程语言中,术语mixin是用来指一个以同样方式使用的结构;向类中添加一堆东西。
在D中(顺便提一下,它不做MI),这是通过将一个模板(考虑语法意识和安全宏,您将接近)插入一个作用域来完成的。这允许在类、结构、函数、模块或任何东西中的一行代码扩展为任意数量的声明。
其他回答
Mixin提供了一种在类中添加功能的方法,即您可以通过将模块包含在所需的类中来与模块中定义的方法进行交互。虽然ruby不支持多重继承,但提供了mixin作为实现这一目标的替代方案。
下面是一个示例,说明如何使用mixin实现多重继承。
module A # you create a module
def a1 # lets have a method 'a1' in it
end
def a2 # Another method 'a2'
end
end
module B # let's say we have another module
def b1 # A method 'b1'
end
def b2 #another method b2
end
end
class Sample # we create a class 'Sample'
include A # including module 'A' in the class 'Sample' (mixin)
include B # including module B as well
def S1 #class 'Sample' contains a method 's1'
end
end
samp = Sample.new # creating an instance object 'samp'
# we can access methods from module A and B in our class(power of mixin)
samp.a1 # accessing method 'a1' from module A
samp.a2 # accessing method 'a2' from module A
samp.b1 # accessing method 'b1' from module B
samp.b2 # accessing method 'a2' from module B
samp.s1 # accessing method 's1' inside the class Sample
这个答案旨在通过以下例子解释mixin:
自包含:简短,不需要知道任何库来理解示例。 Python,而不是其他语言。 可以理解,这里有来自其他语言(如Ruby)的例子,因为这个术语在这些语言中更常见,但这是一个Python线程。
它还应审议有争议的问题:
对于mixin的特征来说,多重继承是必要的还是不必要的?
定义
我还没有看到一个“权威”来源的引用清楚地说明什么是Python中的mixin。
我已经看到了mixin的两种可能的定义(如果它们被认为不同于其他类似的概念,如抽象基类),人们并不完全同意哪一种是正确的。
不同语言之间的共识可能有所不同。
定义1:无多重继承
mixin是这样一个类,该类的一些方法使用了类中没有定义的方法。
因此,该类并不意味着要被实例化,而是作为基类使用。否则,实例将具有在不引发异常的情况下无法调用的方法。
一些源代码添加的约束是类不能包含数据,只能包含方法,但我不明白为什么这是必要的。然而在实践中,许多有用的mixin没有任何数据,没有数据的基类使用起来更简单。
一个经典的例子是从<=和==中实现所有比较运算符:
class ComparableMixin(object):
"""This class has methods which use `<=` and `==`,
but this class does NOT implement those methods."""
def __ne__(self, other):
return not (self == other)
def __lt__(self, other):
return self <= other and (self != other)
def __gt__(self, other):
return not self <= other
def __ge__(self, other):
return self == other or self > other
class Integer(ComparableMixin):
def __init__(self, i):
self.i = i
def __le__(self, other):
return self.i <= other.i
def __eq__(self, other):
return self.i == other.i
assert Integer(0) < Integer(1)
assert Integer(0) != Integer(1)
assert Integer(1) > Integer(0)
assert Integer(1) >= Integer(1)
# It is possible to instantiate a mixin:
o = ComparableMixin()
# but one of its methods raise an exception:
#o != o
这个特殊的例子可以通过functools. total_ordered()装饰器来实现,但这里的游戏是重新发明轮子:
import functools
@functools.total_ordering
class Integer(object):
def __init__(self, i):
self.i = i
def __le__(self, other):
return self.i <= other.i
def __eq__(self, other):
return self.i == other.i
assert Integer(0) < Integer(1)
assert Integer(0) != Integer(1)
assert Integer(1) > Integer(0)
assert Integer(1) >= Integer(1)
定义2:多重继承
mixin是一种设计模式,其中基类的一些方法使用了它没有定义的方法,并且该方法应该由另一个基类实现,而不是像定义1中那样由派生类实现。
术语mixin类指的是打算在该设计模式中使用的基类(TODO是使用该方法的基类,还是实现该方法的基类?)
判断一个给定的类是否为mixin并不容易:方法可以只是在派生类上实现,在这种情况下,我们回到定义1。你必须考虑作者的意图。
这种模式很有趣,因为它可以用不同的基类选择重新组合功能:
class HasMethod1(object):
def method(self):
return 1
class HasMethod2(object):
def method(self):
return 2
class UsesMethod10(object):
def usesMethod(self):
return self.method() + 10
class UsesMethod20(object):
def usesMethod(self):
return self.method() + 20
class C1_10(HasMethod1, UsesMethod10): pass
class C1_20(HasMethod1, UsesMethod20): pass
class C2_10(HasMethod2, UsesMethod10): pass
class C2_20(HasMethod2, UsesMethod20): pass
assert C1_10().usesMethod() == 11
assert C1_20().usesMethod() == 21
assert C2_10().usesMethod() == 12
assert C2_20().usesMethod() == 22
# Nothing prevents implementing the method
# on the base class like in Definition 1:
class C3_10(UsesMethod10):
def method(self):
return 3
assert C3_10().usesMethod() == 13
权威的Python事件
在官方的收藏文档展上。abc的文档明确使用术语Mixin方法。
它指出,如果一个类:
实现__next__ 继承自单个类Iterator
然后该类免费获得一个__iter__ mixin方法。
因此,至少在文档的这一点上,mixin不需要多重继承,并且与定义1一致。
文档在不同的地方当然可能是矛盾的,其他重要的Python库可能在它们的文档中使用其他定义。
本页还使用了术语Set mixin,这清楚地表明像Set和Iterator这样的类可以称为mixin类。
其他语言
Ruby:很明显,mixin不需要多重继承,就像主要的参考书如Programming Ruby和The Ruby Programming Language中提到的那样 c++:设为=0的虚方法是纯虚方法。 定义1与抽象类(具有纯虚方法的类)的定义一致。 该类不能被实例化。 定义2可以通过虚拟继承实现:两个派生类的多重继承
我认为之前的回答很好地定义了mixin是什么。然而, 为了更好地理解它们,从代码/实现的角度将mixin与抽象类和接口进行比较可能是有用的:
1. 抽象类
类,该类需要包含一个或多个抽象方法 抽象类可以包含状态(实例变量)和非抽象方法
2. 接口
接口只包含抽象方法(没有非抽象方法和内部状态)
3.mixin
mixin(像接口)不包含内部状态(实例变量) mixin包含一个或多个非抽象方法(与接口不同,它们可以包含非抽象方法)
在例如Python中,这些只是约定,因为上面所有的都被定义为类。然而,抽象类、接口和mixin的共同特征是它们不应该独立存在,也就是说不应该被实例化。
mixin是一种特殊的多重继承。使用mixin的主要情况有两种:
您希望为类提供大量可选特性。 你想在很多不同的类中使用一个特定的特性。
对于第一个例子,考虑werkzeug的请求和响应系统。我可以这样创建一个普通的request对象:
from werkzeug import BaseRequest
class Request(BaseRequest):
pass
如果我想添加接受报头支持,我会这样做
from werkzeug import BaseRequest, AcceptMixin
class Request(AcceptMixin, BaseRequest):
pass
如果我想让一个请求对象支持接受头,标签,身份验证和用户代理支持,我可以这样做:
from werkzeug import BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin
class Request(AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin, BaseRequest):
pass
区别是微妙的,但是在上面的例子中,mixin类并不是独立存在的。在更传统的多重继承中,AuthenticationMixin(例如)可能更像Authenticator。也就是说,这个类可能被设计成独立存在的。
OP提到他/她从未听说过c++中的mixin,可能是因为它们在c++中被称为奇怪的重复模板模式(CRTP)。另外,@Ciro Santilli提到,mixin在c++中是通过抽象基类实现的。虽然可以使用抽象基类来实现mixin,但这是一种过度使用,因为运行时的虚函数功能可以在编译时使用模板来实现,而不需要在运行时查找虚表。
这里将详细描述CRTP模式
我已经使用下面的模板类将@Ciro Santilli的回答中的python示例转换为c++:
#include <iostream>
#include <assert.h>
template <class T>
class ComparableMixin {
public:
bool operator !=(ComparableMixin &other) {
return ~(*static_cast<T*>(this) == static_cast<T&>(other));
}
bool operator <(ComparableMixin &other) {
return ((*(this) != other) && (*static_cast<T*>(this) <= static_cast<T&>(other)));
}
bool operator >(ComparableMixin &other) {
return ~(*static_cast<T*>(this) <= static_cast<T&>(other));
}
bool operator >=(ComparableMixin &other) {
return ((*static_cast<T*>(this) == static_cast<T&>(other)) || (*(this) > other));
}
protected:
ComparableMixin() {}
};
class Integer: public ComparableMixin<Integer> {
public:
Integer(int i) {
this->i = i;
}
int i;
bool operator <=(Integer &other) {
return (this->i <= other.i);
}
bool operator ==(Integer &other) {
return (this->i == other.i);
}
};
int main() {
Integer i(0) ;
Integer j(1) ;
//ComparableMixin<Integer> c; // this will cause compilation error because constructor is protected.
assert (i < j );
assert (i != j);
assert (j > i);
assert (j >= i);
return 0;
}
编辑:在ComparableMixin中添加了受保护的构造函数,这样它只能被继承而不能被实例化。更新了示例,以显示在创建ComparableMixin对象时protected构造函数将如何导致编译错误。