dictionary and __getattr__

Michael Abbott michael at rcp.co.uk
Thu Sep 6 06:24:44 EDT 2001


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.



More information about the Python-list mailing list