gpt4 book ai didi

python - 完美模仿继承与组合

转载 作者:太空宇宙 更新时间:2023-11-03 14:54:39 25 4
gpt4 key购买 nike

我试图以这样的方式包装第三方包中的类,使我的新类看起来与第三方类的子类完全相同。第三方类支持继承,并且具有重要的功能,例如具有 __getitem__ 方法的函数。我可以使用基于 Wrapping a class whose methods return instances of that classHow can I intercept calls to python's "magic" methods in new style classes? 的解决方案包装几乎每个属性和方法。不过,我还是需要重写第三方类的__init__方法。我怎样才能做到这一点?注意:我正在使用新式类。

到目前为止的代码:

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'>

我尝试通过在 __init__ 中定义 WrapperMetaclass 的新代理来进行覆盖,但很快遇到了无限递归。

我的代码库很复杂,用户的技能水平各不相同,因此我无法使用猴子修补或修改示例类 ClassToWrapWrapped 定义的解决方案。我真的希望对上面的代码进行扩展来覆盖 Wrapped.__init__

请注意,这个问题不仅仅是例如的重复。 Can I exactly mimic inheritance behavior with delegation by composition in Python? 。该帖子没有提供任何与我已在此处提供的内容一样详细的答案。

最佳答案

听起来你只是想要 Wrapper.__init__方法与目前的工作方式不同。而不是采用 __wraps__ 的现有实例类,它应该采用另一个类在其构造函数中期望的参数并为您构建实例。尝试这样的事情:

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

关于python - 完美模仿继承与组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45679642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com