我发现this recipe创建一个代理类。我用它来包装自定义对象,并且想要重载某些属性并将新属性附加到代理上。然而,当我在代理上调用任何方法时(从代理类中),我最终被委托给了不是我想要的包装。Python对象代理:如何访问代理
有什么方法可以访问或存储对代理的引用吗?
下面是一些代码(未经测试)来演示问题。
class MyObject(object):
@property
def value(self):
return 42
class MyObjectProxy(Proxy): # see the link above
def __getattribute__(self, attr):
# the problem is that `self` refers to the proxied
# object and thus this throws an AttributeError. How
# can I reference MyObjectProxy.another_value()?
if attr == 'value': return self.another_value() # return method or attribute, doesn't matter (same effect)
return super(MyObjectProxy, self).__getattribute__(attr)
def another_value(self):
return 21
o = MyObject()
p = MyObjectProxy(o)
print o.value
print p.value
从某种意义上说我的问题是,代理工作太出色了,隐藏所有自己的方法/属性,并冒充自己作为代理对象(这是它应该做的)...
更新
基于下面的评论,我改变__getattribute__
这样:
def __getattribute__(self, attr):
try:
return object.__getattribute__(self, attr)
except AttributeError:
return super(MyObjectProxy, self).__getattribute__(attr)
这看ms现在就可以完成这个技巧,但最好直接添加到Proxy
类中。
感谢您的再现和确认,@张扬余。有关如何获得所需行为的任何建议? – orange
我已经更新了我的答案。请检查。 @orange – zhangyangyu
我以为我曾尝试过,但我一定犯了一个错误。你的解决方案工作正常在查看代理人的代码之前,我先看看如何修改代理类来查找自己的属性。 – orange