which "dictionary with attribute-style access"?

Terry Reedy tjreedy at udel.edu
Sun Oct 18 00:24:09 CEST 2009

Aahz wrote:
> In article <hb01po$s5v$1 at news.eternal-september.org>,
> Andreas Balogh  <baloand at gmail.com> wrote:
>> My question to the Python specialists: which one is the most correct?
>> Are there restrictions with regards to pickling or copy()?
>> Which one should I choose?
> What's your goal?  I'd probably do the dirt simple myself:
> class AttrDict(dict):
>     def __getattr__(self, attr):
>         if attr in self:
>             return self[attr]
>         else:
>             raise AttributeError

Why the double lookup? Harking to another thread on using exceptions,

     return self[attr]
except KeyError:
     raise AttributeError(attr)

>     def __setattr__(self, attr, value):
>         self[attr] = value
> d = AttrDict()
> d.foo = 'bar'
> print d.foo

More information about the Python-list mailing list