在Python编程中,Mark Lutz提到了术语mixin。我有C/ c++ / c#的背景,以前没有听说过这个术语。什么是mixin?

从这个例子的字里行间(我链接了这个例子,因为它很长),我假设这是一个使用多重继承来扩展类的情况,而不是适当的子类化。这样对吗?

为什么我要这样做,而不是把新功能放到一个子类?就此而言,为什么混合/多重继承方法会比使用组合更好呢?

mixin与多重继承的区别是什么?这只是语义上的问题吗?


当前回答

这个概念来自Steve 's Ice Cream,这是一家由Steve Herrell于1973年在马萨诸塞州萨默维尔创立的冰淇淋店,在那里混合(糖果、蛋糕等)被混合成基本的冰淇淋口味(香草、巧克力等)。

受到Steve 's Ice Cream的启发,Lisp对象系统Flavors的设计者第一次在编程语言中包含了这个概念,其中mix-in是用于增强其他类的小型辅助类,flavour是大型独立类。

因此,主要的思想是mix-in是一个可重用的扩展(“可重用”而不是“独占”;' extension '而不是' base ')。

这个概念与单一或多个继承以及抽象或具体类的概念是正交的。混合类可以用于单个或多个继承,可以是抽象类也可以是具体类。混合类有不完整的接口,抽象类有不完整的实现,具体类有完整的实现。

Mix-in类名通常以' -MixIn '、' -able '或' -ible '作为后缀,以强调它们的性质,就像Python标准库中socketserver模块的ThreadingMixIn和ForkingMixIn类,以及集合的Hashable、Iterable、Callable、Awaitable、AsyncIterable和可逆类一样。美国广播公司(abc)模块。

下面是一个mix-in类的例子,用于扩展Python内置list和dict类的日志记录功能:

import logging

class LoggingMixIn:
    def __setitem__(self, key, value):
        logging.info('Setting %r to %r', key, value)
        super().__setitem__(key, value)
    def __delitem__(self, key):
        logging.info('Deleting %r', key)
        super().__delitem__(key)

class LoggingList(LoggingMixIn, list):
    pass

class LoggingDict(LoggingMixIn, dict):
    pass
>>> logging.basicConfig(level=logging.INFO)
>>> l = LoggingList([False])
>>> d = LoggingDict({'a': False})
>>> l[0] = True
INFO:root:Setting 0 to True
>>> d['a'] = True
INFO:root:Setting 'a' to True
>>> del l[0]
INFO:root:Deleting 0
>>> del d['a']
INFO:root:Deleting 'a'

其他回答

mixin与多重继承的区别是什么?这只是语义上的问题吗?

mixin是一种有限形式的多重继承。在某些语言中,向类中添加mixin的机制(就语法而言)与继承的机制略有不同。

特别是在Python上下文中,mixin是一个父类,它为子类提供功能,但并不打算自己被实例化。

你可能会说,“这只是多重继承,而不是真正的mixin”,因为可能被混淆为mixin的类实际上可以被实例化和使用——所以这确实是语义上的,而且非常真实的区别。

多重继承示例

这个例子,来自文档,是一个OrderedCounter:

类OrderedCounter(计数器,OrderedDict): “计数器,记住第一次遇到的顺序元素” def __repr__(自我): 返回'%s(%r)' % (self.__class__. zip)。__name__ OrderedDict(自我) def __reduce__(自我): 回归自我。__class__进行(OrderedDict(自我),)

它从集合模块继承了Counter和OrderedDict。

Counter和OrderedDict都打算被实例化并单独使用。然而,通过将它们都子类化,我们可以拥有一个有序的计数器,并重用每个对象中的代码。

这是一种重用代码的强大方法,但也可能产生问题。如果发现其中一个对象存在bug,那么不加注意地修复它可能会在子类中创建一个bug。

Mixin的例子

mixin通常被推广为一种获得代码重用的方式,而不会出现协作多重继承(如OrderedCounter)可能存在的潜在耦合问题。当您使用mixin时,您使用的功能与数据不是紧密耦合的。

与上面的例子不同,mixin不打算单独使用。它提供了新的或不同的功能。

例如,标准库在socketserver库中有两个mixin。

可以创建每种类型服务器的分叉和线程版本 使用这些混合类。例如,ThreadingUDPServer是 创建如下: 类ThreadingMixIn, UDPServer: 通过 mix-in类首先出现,因为它覆盖了定义在 UDPServer。设置各种属性还会改变的行为 底层服务器机制。

在这种情况下,mixin方法覆盖UDPServer对象定义中的方法,以实现并发性。

重写的方法似乎是process_request,它还提供了另一个方法process_request_thread。这是来自源代码:

class ThreadingMixIn: """Mix-in class to handle each request in a new thread.""" # Decides how threads will act upon termination of the # main process daemon_threads = False def process_request_thread(self, request, client_address): """Same as in BaseServer but as a thread. In addition, exception handling is done here. """ try: self.finish_request(request, client_address) except Exception: self.handle_error(request, client_address) finally: self.shutdown_request(request) def process_request(self, request, client_address): """Start a new thread to process the request.""" t = threading.Thread(target = self.process_request_thread, args = (request, client_address)) t.daemon = self.daemon_threads t.start()

一个虚构的例子

这是一个主要用于演示目的的mixin -大多数对象将会进化到超出这个repr的有用性:

class SimpleInitReprMixin(object):
    """mixin, don't instantiate - useful for classes instantiable
    by keyword arguments to their __init__ method.
    """
    __slots__ = () # allow subclasses to use __slots__ to prevent __dict__
    def __repr__(self):
        kwarg_strings = []
        d = getattr(self, '__dict__', None)
        if d is not None:
            for k, v in d.items():
                kwarg_strings.append('{k}={v}'.format(k=k, v=repr(v)))
        slots = getattr(self, '__slots__', None)
        if slots is not None:
            for k in slots:
                v = getattr(self, k, None)
                kwarg_strings.append('{k}={v}'.format(k=k, v=repr(v)))
        return '{name}({kwargs})'.format(
          name=type(self).__name__,
          kwargs=', '.join(kwarg_strings)
          )

用法是:

class Foo(SimpleInitReprMixin): # add other mixins and/or extend another class here
    __slots__ = 'foo',
    def __init__(self, foo=None):
        self.foo = foo
        super(Foo, self).__init__()

和用法:

>>> f1 = Foo('bar')
>>> f2 = Foo()
>>> f1
Foo(foo='bar')
>>> f2
Foo(foo=None)

这不是一个Python的例子,但在D编程语言中,术语mixin是用来指一个以同样方式使用的结构;向类中添加一堆东西。

在D中(顺便提一下,它不做MI),这是通过将一个模板(考虑语法意识和安全宏,您将接近)插入一个作用域来完成的。这允许在类、结构、函数、模块或任何东西中的一行代码扩展为任意数量的声明。

大致总结一下上面所有的好答案:

                States        /     Methods Concrete Method Abstract Method
Concrete State Class Abstract Class
Abstract State Mixin Interface

我建议不要在新的Python代码中使用mix-in,如果你能找到其他方法(比如用复合代替继承,或者只是把方法打补丁到你自己的类中),这样做并不费力。

在老式的类中,您可以使用mix- In作为从另一个类中抓取一些方法的一种方式。但在新风格的世界里,一切,甚至是混合,都继承自对象。这意味着任何多重继承的使用都会自然地引入MRO问题。

在Python中有很多方法可以使多继承MRO工作,最著名的是super()函数,但这意味着您必须使用super()来完成整个类层次结构,并且要理解控制流要困难得多。

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