I need to implement such behavior:
obj.attr1.attr2.attr3 --> obj.attr1__attr2__attr3
It looks like I have to override obj's class __getattribute__ and also use python descriptors somehow.
UPDATE:
I have a django project.
obj is django-haystack's SearchResult instance, it contains a lot of de-normalized data (user__name, user__address) from django model, and I need to access it as result.user.name for compatibility reasons.
UPDATE for THC4k's answer:
What if I have:
class Target(object):
attr1 = 1
attr1__attr2__attr3 = 5
>>> proxy.attr1
1
>>> proxy.attr1.attr2.attr3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'attr2'
Any help will be much appreciated.
obj.attr1__attr2exists andobj.attr1has an attributeattr2? The results are ambiguous.obj.attr1.attr2when bothobj.attr1andobj.attr1__attr2exists is again ambiguous. Worse, what do you expect to happen if you sayobj.attr1whenobj.attr1__attr2exists, butobj.attr1doesn't? It's impossible to makea.braise an AttributeError anda.b.cnot. This is inherently broken.