AtrributeDict

alex23 wuwei23 at gmail.com
Thu Apr 30 01:52:15 EDT 2009


On Apr 30, 9:09 am, Дамјан Георгиевски <gdam... at gmail.com> wrote:
> I've needed an attribute accessible dict, so I created this.
> Are there any obviously stupid shortcomings?
>
> class AttrDict(dict):
>    def __getattr__(self, name):
>        try:
>            return self[name]
>        except KeyError, e:
>            raise AttributeError(e)

Have you seen Alex Martelli's Bunch class?

class Bunch:
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

>From http://code.activestate.com/recipes/52308/

With Alex' version, you're not overloading a commonly called method,
nor do you have to worry about the performance of the exception
handling for misses. Plus it's half as long ;)




More information about the Python-list mailing list