What is a mixin, and why are they useful?

后端 未结 16 2124
無奈伤痛
無奈伤痛 2020-11-22 00:18

In \"Programming Python\", Mark Lutz mentions \"mixins\". I\'m from a C/C++/C# background and I have not heard the term before. What is a mixin?

Reading between the

16条回答
  •  盖世英雄少女心
    2020-11-22 01:05

    A mixin is a special kind of multiple inheritance. There are two main situations where mixins are used:

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

    For an example of number one, consider werkzeug's request and response system. I can make a plain old request object by saying:

    from werkzeug import BaseRequest
    
    class Request(BaseRequest):
        pass
    

    If I want to add accept header support, I would make that

    from werkzeug import BaseRequest, AcceptMixin
    
    class Request(AcceptMixin, BaseRequest):
        pass
    

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

    from werkzeug import BaseRequest, AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin
    
    class Request(AcceptMixin, ETagRequestMixin, UserAgentMixin, AuthenticationMixin, BaseRequest):
        pass
    

    The difference is subtle, but in the above examples, the mixin classes weren't made to stand on their own. In more traditional multiple inheritance, the AuthenticationMixin (for example) would probably be something more like Authenticator. That is, the class would probably be designed to stand on its own.

提交回复
热议问题