Using dict as object

Pierre Tardy tardyp at gmail.com
Wed Sep 19 12:19:22 EDT 2012


>
>  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

> I would like to have python guys advices on how one could optimize this.
>
> Use C code and slots.
>
I tried adding __slots__= [], to my class, but my benchmarks do not show
significant changes.


if you're proposing a new module for the stdlib, one of the (unstated?)
> requirements is that it be in regular use by a fairly large audience for
> a while.
>
I was talking about escalading to python-dev as a way to hit more
expertize, but it looks like this one is already
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120919/c6321964/attachment.html>


More information about the Python-list mailing list