On 22 nov, 21:44, Roman Dolgiy <tost...@gmail.com> wrote: >> http://stackoverflow.com/questions/4247036/python-recursively-getattr... > > I need to support a lot of legacy code, with THC4k's approach I'll > have to modify project's existing code to use obj.attr1.val instead of > obj.attr1 but this is not suitable.
You should probably re-read THC4k's answer. His code works just fine AFAICT: """ # the proxy maps attribute access to another object class GetattrProxy(object): def __init__(self, proxied, prefix=None): self.proxied = proxied self.prefix = prefix def __getattr__(self, key): attr = (key if self.prefix is None else self.prefix + '__' + key) try: # if the proxied object has the attr return it return getattr(self.proxied, attr) except AttributeError: # else just return another proxy return GetattrProxy(self.proxied, attr) # the thing you want to wrap class Target(object): attr1__attr2__attr3 = 5 attr2 = "attr2" t = Target() proxy = GetattrProxy(t) print "proxy.attr1.attr2.attr3 : '%s'" % proxy.attr1.attr2.attr3 print "proxy.attr2 : '%s'" % proxy.attr2 """ -- http://mail.python.org/mailman/listinfo/python-list