which "dictionary with attribute-style access"?

Aahz aahz at pythoncraft.com
Sat Oct 17 18:32:45 EDT 2009


In article <mailman.1614.1255818306.2807.python-list at python.org>,
Terry Reedy  <tjreedy at udel.edu> wrote:
>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,
>
>try:
>     return self[attr]
>except KeyError:
>     raise AttributeError(attr)

For this purpose, it's almost entirely a stylistic difference; I happen
to prefer using the test, but the other way is fine, too.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"To me vi is Zen.  To use vi is to practice zen.  Every command is a
koan.  Profound to the user, unintelligible to the uninitiated.  You
discover truth everytime you use it."  --reddy at lion.austin.ibm.com



More information about the Python-list mailing list