Getting a dictionary from an object

Paolino paolo_veronelli at tiscali.it
Sat Jul 23 06:06:57 EDT 2005


Thanos Tsouanas wrote:
> Hello.
> 
> I would like to have a quick way to create dicts from object, so that a
> call to foo['bar'] would return obj.bar.
> 
> The following works, but I would prefer to use a built-in way if one
> exists.  Is there one?
> 

> class dictobj(dict):
>     """
>     class dictobj(dict):
>     A dictionary d with an object attached to it,
> 	which treats d['foo'] as d.obj.foo.
>     """
>     def __init__(self, obj):
>         self.obj = obj
>     def __getitem__(self, key):
>         return self.obj.__getattribute__(key)
use getattr(self.obj,key) possibly, as __getattribute__ gets total 
control on attribute access

	

	
		
___________________________________ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it



More information about the Python-list mailing list