namespace dictionaries ok?

Ron Adam rrr at ronadam.com
Tue Oct 25 02:54:09 EDT 2005


James Stroud wrote:
> Here it goes with a little less overhead:
> 
> 
> py> class namespace:
> ...   def __init__(self, adict):
> ...     self.__dict__.update(adict)
> ...
> py> n = namespace({'bob':1, 'carol':2, 'ted':3, 'alice':4})
> py> n.bob
> 1
> py> n.ted
> 3
> 
> James

How about...

     class namespace(dict):
         __getattr__ = dict.__getitem__
         __setattr__ = dict.__setitem__
         __delattr__ = dict.__delitem__


This seems to work, and eliminates the indirect method calls.

Cheers,
    Ron




More information about the Python-list mailing list