The reason that your code goes wrong is the loop in __getattribute__
. You want to override __getattribute__
so you can reach certain properties in the proxy class itself. But let's see.
When you call p.value
the __getattribute__
is called. Then it comes here if attr == 'value': return self.another_value()
. Here we need to call another_value
so we enter __getattribute__
again.
This time we comes here return super(MyObjectProxy, self).__getattribute__(attr)
. We call the Proxy
's __getattribute__
, and it tries to fetch another_value
in Myobject
. So the exceptions occur.
You can see from the traceback that we finally goes to return super(MyObjectProxy, self).__getattribute__(attr)
that should not go to.
Traceback (most recent call last):
File "proxytest.py", line 22, in <module>
print p.value
File "proxytest.py", line 13, in __getattribute__
if attr == 'value': return self.another_value() # return method or attribute, doesn't matter (same effect)
File "proxytest.py", line 14, in __getattribute__
return super(MyObjectProxy, self).__getattribute__(attr)
File "/home/hugh/m/tspace/proxy.py", line 10, in __getattribute__
return getattr(object.__getattribute__(self, "_obj"), name)
AttributeError: 'MyObject' object has no attribute 'another_value'
edit:
Change the line of code if attr == 'value': return self.another_value()
to if attr == 'value': return object.__getattribute__(self, 'another_value')()
.