Overview:
class Inner(object): def __init__(self, x): self.x = x class Outer(object): def __init__(self, z): self.inner = Inner(z) o = Outer(10)
Now I want the Outer object to behave transparently - any attributes set to o
should be set to o.inner
, the same for reading: o.something
should really return o.inner.sommething
. A kind of proxy or relay.
__getattr__
for Outer
seems simple and works:
def __getattr__(self, item): return getattr(self, item)
How do I handle __setattr__
? I could not think of anything that would not cause a recursion error and make me hate.
Or is the concept itself erroneous?
(The other approach I tried was that Outer
is a subclass of Inner
- it really didn't play aesthetically true @classmethods
, not to mention that the IDE would be lost inside them - would not be able to solve some attributes. Let me leave it now , perhaps?)
source share