How does the order of mixins affect the derived class?
比如说,我有以下通过触摸
1 2 3 4 5 6 7 8 9 10 11 | 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) |
如果我想查看订单,请检查a->check b,我的代码应该是
为什么我们总是把
MRO基本上是深度优先,从左到右。有关更多信息,请参见新样式Python类中的方法解析顺序(MRO)。
您可以查看要检查的类的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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() |
印刷品:
1 2 3 | perform check A perform check B base dispatch |