python - 用组合完美地模拟继承

python - perfectly mimic inheritance with composition

我正在尝试包装来自第三方包的 class,使我的新 class 看起来完全像第三方 class 的子 class =40=]。第三方 class 支持继承,并且它具有重要的功能,例如具有 __getitem__ 方法的函数。我可以使用基于 and How can I intercept calls to python's "magic" methods in new style classes? 的解决方案来包装几乎每个属性和方法。但是,我还是需要重写第三方class的__init__方法。我怎样才能做到这一点?注意:我使用的是新式 classes.

到目前为止的代码:

import copy

class WrapperMetaclass(type):
    """
    Works with the `Wrapper` class to create proxies for the wrapped object's magic methods.
    """
    def __init__(cls, name, bases, dct):

        def make_proxy(name):
            def proxy(self, *args):
                return getattr(self._obj, name)
            return proxy

        type.__init__(cls, name, bases, dct)
        if cls.__wraps__:
            ignore = set("__%s__" % n for n in cls.__ignore__.split())
            for name in dir(cls.__wraps__):
                if name.startswith("__"):
                    if name not in ignore and name not in dct:
                        setattr(cls, name, property(make_proxy(name)))

class Wrapper(object):
    """
    Used to provide a (nearly) seamless inheritance-like interface for classes that do not support direct inheritance.
    """

    __metaclass__ = WrapperMetaclass
    __wraps__  = None
    # note that the __init__ method will be ignored by WrapperMetaclass
    __ignore__ = "class mro new init setattr getattr getattribute dict"

    def __init__(self, obj):
        if self.__wraps__ is None:
            raise TypeError("base class Wrapper may not be instantiated")
        elif isinstance(obj, self.__wraps__):
            self._obj = obj
        else:
            raise ValueError("wrapped object must be of %s" % self.__wraps__)

    def __getattr__(self, name):
        if name is '_obj':
            zot = 1
        orig_attr = self._obj.__getattribute__(name)
        if callable(orig_attr) and not hasattr(orig_attr, '__getitem__'):
            def hooked(*args, **kwargs):
                result = orig_attr(*args, **kwargs)
                if result is self._obj:
                    return self
                elif isinstance(result, self.__wraps__):
                    return self.__class__(result)
                else:
                    return result
            return hooked
        else:
            return orig_attr

    def __setattr__(self, attr, val):
        object.__setattr__(self, attr, val)
        if getattr(self._obj, attr, self._obj) is not self._obj: # update _obj's member if it exists
            setattr(self._obj, attr, getattr(self, attr))

class ClassToWrap(object):
    def __init__(self, data):
        self.data = data

    def theirfun(self):
        new_obj = copy.deepcopy(self)
        new_obj.data += 1
        return new_obj

    def __str__(self):
        return str(self.data)

class Wrapped(Wrapper):
    __wraps__ = ClassToWrap

    def myfun(self):
        new_obj = copy.deepcopy(self)
        new_obj.data += 1
        return new_obj

# can't instantiate Wrapped directly! This is the problem!
obj = ClassToWrap(0)
wr0 = Wrapped(obj)
print wr0
>> 0
print wr0.theirfun()
>> 1

这可行,但对于真正无缝的类继承行为,我需要直接实例化 Wrapped,例如

wr0 = Wrapped(0)

当前抛出

ValueError: wrapped object must be of <class '__main__.ClassToWrap'>

我试图通过在 WrapperMetaclass 中为 __init__ 定义一个新代理来覆盖,但很快 运行 进入无限递归。

我的代码库很复杂,包含不同技能水平的用户,所以我无法使用猴子补丁或修改示例定义的解决方案 classes ClassToWrapWrapped。我真的希望对覆盖 Wrapped.__init__ 的上述代码进行扩展。

请注意,这个问题不仅仅是例如Can I exactly mimic inheritance behavior with delegation by composition in Python?。 post 没有任何答案几乎和我在这里提供的一样详细。

听起来您只是希望 Wrapper.__init__ 方法以不同于当前的方式工作。与其采用 __wraps__ class 的现有实例,不如采用另一个 class 在其构造函数中期望的参数并为您构建实例。尝试这样的事情:

def __init__(self, *args, **kwargs):
    if self.__wraps__ is None:
        raise TypeError("base class Wrapper may not be instantiated")
    else:
        self._obj = self.__wraps__(*args, **kwargs)

如果您出于某种原因希望 Wrapper 保持不变,您可以将逻辑放在新的 Wrapped.__init__ 方法中:

def __init__(self, data): # I'm explicitly naming the argument here, but you could use *args
    super(self, Wrapped).__init__(self.__wraps__(data)) # and **kwargs to make it extensible