dictionary and __getattr__

Chris Ryland cpr at emsoftware.com
Thu Sep 6 10:14:59 EDT 2001


Michael Abbott <michael at rcp.co.uk> wrote in message news:<Xns91147473F9B1Emichaelrcpcouk at 194.238.50.13>...
> Harald Kirsch <kirschh at lionbioscience.com> wrote in 
> news:yv28zfsr6e8.fsf at lionsp093.lion-ag.de:
> 
> > 
> > 
> > Wouldn't it be nice if this would work:
> > 
> >   d = {'x': 1}
> >   print d.x
> > 
> > i.e. every entry in a dictionary is also an attribute of the
> > dictionary itself. 
> > 
> > Is there a class wrapper for that somewhere available?
> 
> I expect this would work:
> 
>     	class Wrap:
>     	    def __init__(self, dict):
>     	        self.__dict__.update(dict)
> 
>     	d = {'x': 1}
>     	dd = Wrap(d)
>     	print dd.x
> 
> Of course, this may not be exactly what you want, because dd won't follow 
> updates to d.

Hey, won't the new 2.2 support for extending built-in classes such as
dictionaries make this both easy and complete?

--Chris Ryland, Em Software



More information about the Python-list mailing list