What is a mixin, and why are they useful?

后端 未结 16 2118
無奈伤痛
無奈伤痛 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:17

    Maybe an example from ruby can help:

    You can include the mixin Comparable and define one function "<=>(other)", the mixin provides all those functions:

    <(other)
    >(other)
    ==(other)
    <=(other)
    >=(other)
    between?(other)
    

    It does this by invoking <=>(other) and giving back the right result.

    "instance <=> other" returns 0 if both objects are equal, less than 0 if instance is bigger than other and more than 0 if other is bigger.

    0 讨论(0)
  • 2020-11-22 01:17

    mixin gives a way to add functionality in a class, i.e you can interact with methods defined in a module by including the module inside the desired class. Though ruby doesn't supports multiple inheritance but provides mixin as an alternative to achieve that.

    here is an example that explains how multiple inheritance is achieved using 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
    
    0 讨论(0)
  • 2020-11-22 01:19

    I just used a python mixin to implement unit testing for python milters. Normally, a milter talks to an MTA, making unit testing difficult. The test mixin overrides methods that talk to the MTA, and create a simulated environment driven by test cases instead.

    So, you take an unmodified milter application, like spfmilter, and mixin TestBase, like this:

    class TestMilter(TestBase,spfmilter.spfMilter):
      def __init__(self):
        TestBase.__init__(self)
        spfmilter.config = spfmilter.Config()
        spfmilter.config.access_file = 'test/access.db'
        spfmilter.spfMilter.__init__(self)
    

    Then, use TestMilter in the test cases for the milter application:

    def testPass(self):
      milter = TestMilter()
      rc = milter.connect('mail.example.com',ip='192.0.2.1')
      self.assertEqual(rc,Milter.CONTINUE)
      rc = milter.feedMsg('test1',sender='good@example.com')
      self.assertEqual(rc,Milter.CONTINUE)
      milter.close()
    

    http://pymilter.cvs.sourceforge.net/viewvc/pymilter/pymilter/Milter/test.py?revision=1.6&view=markup

    0 讨论(0)
  • 2020-11-22 01:25

    I think of them as a disciplined way of using multiple inheritance - because ultimately a mixin is just another python class that (might) follow the conventions about classes that are called mixins.

    My understanding of the conventions that govern something you would call a Mixin are that a Mixin:

    • adds methods but not instance variables (class constants are OK)
    • only inherits from object (in Python)

    That way it limits the potential complexity of multiple inheritance, and makes it reasonably easy to track the flow of your program by limiting where you have to look (compared to full multiple inheritance). They are similar to ruby modules.

    If I want to add instance variables (with more flexibility than allowed for by single inheritance) then I tend to go for composition.

    Having said that, I have seen classes called XYZMixin that do have instance variables.

    0 讨论(0)
提交回复
热议问题