首页 > 解决方案 > 如何自定义 Python 的方法解析顺序(mro)?

问题描述

我想自定义调用我继承的方法。

这是一个示例代码:

class First(object):
    def get(self):
        print('getting from first')

    def set(self):
        print('setting to first')

class Second(object):
    def get(self):
        print('getting from second')

    def set(self):
        print('setting to second')

class Third(First, Second):
    def get(self):
        super(Third, self).get()

    def set(self):
        super(Third, self).set()

现在我想要的行为是这样的:

third = Third()
third.get() # -> should print 'getting from first'
third.set() # -> should print 'setting to second'

现在mro显示:

Third.__mro__ ->  (__main__.Third, __main__.First, __main__.Second, object)

我们可以看到main .First中的方法总是首先被调用。虽然我想要的是在执行set()方法期间首先调用main .Second 。

这是我解决这个问题的尝试,尝试修改三等MRO

思路是交换两个类的两个位置,看看能不能行。首先,一个swap()辅助函数。

def swap(index1, index2, mro_tuple):
    l = list(mro_tuple)
    temp = l[index1]
    l[index1] = l[index2]
    l[index2] = temp
    return tuple(l)

然后在set()方法的实现过程中,我尝试修改底层类的mro

class Third(First, Second):
    def get(self):
        super(Third, self).get()
    def set(self):
        self.__class__.__mro__ = swap(1, 2, self.__class__.__mro__) # swap here..
        super(Third, self).set() # then call method**
In [43]: third = Third() 

In [44]: third.get()                                                            
getting from first

In [45]: third.set()                                                            
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-53-c82ac1a0d5bc> in <module>
----> 1 third.set()

<ipython-input-50-00c9baff0d57> in set(self)
      4 
      5     def set(self):
----> 6         self.__class__.__mro__ = swap(1, 2, self.__class__.__mro__) # swap here..
      7         super(Third, self).set() # then call method
      8 

AttributeError: readonly attribute

它表明该__mro__属性无法重置。

反正有没有以方便的方式实现这种行为?

标签: pythonmultiple-inheritancemethod-resolution-order

解决方案


您最好的选择可能是Third明确使用该Second实现:

class Third(First, Second):
    set = Second.set

尽管您提出这个问题的事实是一个警告信号,表明您可能选择了错误的类层次结构。


推荐阅读