当派生类重载它并且基类不知道时,如何从基类调用基类方法?

时间:2014-12-08 19:29:28

标签: python class methods derived

在我的代码中,我定义了一个派生自另一个类的类,我无法修改它。我想覆盖该基类的方法,该方法在基类中递归调用自身。不幸的是,这种递归现在调用派生类的新定义方法而不是基类的原始方法。我怎么能告诉python基类必须调用它自己的方法而不是派生的方法而不能修改这个基类?

示例:

class Base(dict):
    ''' defined in a package, cannot modify anything in this class '''

    def func(self, param):
        if (not recursion abort condition):
            # The next line doesn't call Base.func() if someone overloads it in a derived class.
            # How can I achieve that it calls Base.func() every time, regardless of derived classes?
            # This class doesn't know that someone derives it at some time, so modifying it is not an option.
            compute_something += self.func(some_value)
        return stuff

class Derived(Base):
    ''' My class. Here, I have full access '''
    def __init__(self):
        self._lock = <init some mutex>

    def func(self, param):
        # runs into a deadlock here in the second recursion call out of Base.func()
        with (self._lock):  # protect the method of the base class in some way
            return super(Derived, self).func(param)

0 个答案:

没有答案