On 2012-09-19, Pierre Tardy <tar...@gmail.com> wrote: > --===============1362296571== > Content-Type: multipart/alternative; boundary=bcaec554d3229e814204ca105e50 > > --bcaec554d3229e814204ca105e50 > Content-Type: text/plain; charset=ISO-8859-1 > >> >> This has been proposed and discussed and even implemented many >> times on this list and others. >> > I can find this question on SO > http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python > which is basically answered with this solution > > class AttributeDict(dict): > __getattr__ = dict.__getitem__ > __setattr__ = dict.__setitem__ > > > but this does not allow recursive access, you would need to first convert > all nested dictionaries to AttributeDict. > a.b.c.d = 2 # fail > a.b = dict(c=3) > a.b.c=4 # fail
There is no way to control "recursive access" in Python. The statement a.b.c = 2 is equivalent to the statements o = a.b # o = a.__getattr__('b') o.c = 2 # o.__setattr__('c', 2) The way that the o.c assignment is handled is determined by the type of o regardless of the type of a. If you're looking for a way to change only the type of a and make a custom __(set|get)attr__ work for all dicts that are indirectly referred to then there is no solution to your problem. Oscar -- http://mail.python.org/mailman/listinfo/python-list