67

Say, I have the following mixins that overlaps with each other by touching dispatch():

class FooMixin(object):
    def dispatch(self, *args, **kwargs):
        # perform check A
        ...
        return super(FooMixin, self).dispatch(*args, **kwargs)

class BarMixin(object):
    def dispatch(self, *args, **kwargs):
        # perform check B
        ...
        return super(FooMixin, self).dispatch(*args, **kwargs)

If I want my view to go through the order, check A -> check B, should my code be MyView(FooMixin, BarMixin, View) or MyView(BarMixin, FooMixin, View)?

And why do we always put View or its subclasses after mixins? (I have noticed this from reading the source code of the django generic views, but I don't know the rationale behind it, if any)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
tamakisquare
  • 16,659
  • 26
  • 88
  • 129

1 Answers1

105

The MRO is basically depth-first, left-to-right. See Method Resolution Order (MRO) in new style Python classes for some more info.

You can look at the __mro__ attribute of the class to check, but FooMixin should be first if you want to do "check A" first.

class UltimateBase(object):
    def dispatch(self, *args, **kwargs):
        print 'base dispatch'

class FooMixin(object):
    def dispatch(self, *args, **kwargs):
        print 'perform check A'
        return super(FooMixin, self).dispatch(*args, **kwargs)

class BarMixin(object):
    def dispatch(self, *args, **kwargs):
        print 'perform check B'
        return super(BarMixin, self).dispatch(*args, **kwargs)

class FooBar(FooMixin, BarMixin, UltimateBase):
    pass

FooBar().dispatch()

Prints:

perform check A
perform check B
base dispatch

View has to be last so that it "catches" any attribute lookups that weren't on any mixins, without hiding any methods on those mixins. I'm not sure I understand that part of your question -- what it "why is it added at all" or "why is it added last"?

Community
  • 1
  • 1
agf
  • 171,228
  • 44
  • 289
  • 238
  • 2
    thx agf. My question was meant to be "why is it added to the last" and you have answered it. Cheers. – tamakisquare Apr 05 '12 at 18:05
  • 4
    Just to be clear, the only method that this calls directly is `FooMixin.dispatch`. `super(FooMixin, self).dispatch` then evaluates to `BarMixin.dispatch` because `object` does not have a `dispatch` method. `super(BarMixin, self).dispatch` evaluates to `UltimateBase.dispatch` for the same reason. – Mad Physicist Jan 31 '18 at 21:48
  • @MadPhysicist That's not quite right. This will work even if the method is one also defined by object -- try it yourself. See the linked answer for more information. – agf Feb 01 '18 at 02:10
  • @agf. Of course. My mistake. `object`'s dispatch would be called last. My statement was really supposed to be a question but I got distracted and forgot to make that explicit. Thanks for answering anyway :) – Mad Physicist Feb 01 '18 at 02:26