object attributes from a dictionary

Darren Dale dd55 at cornell.edu
Wed Jul 14 10:51:28 EDT 2004


John Lenton wrote:
> On Tue, 13 Jul 2004 18:24:49 -0400, Darren Dale <dd55 at cornell.edu> wrote:
> 
>>Is there a more direct way than this to turn a dictionary into an object?
>>
>>class pupa:
>>     def __init__(self,initDict,*args,**kwargs):
>>         [setattr(self,key,initDict[key]) for key in initDict.keys()]
>>
>>larva={'a':1,'b':2}
>>moth=pupa(larva)
>>
>>(ok, so I'm dorking a bit here. I havent slept in two days.)
> 
> 
>     class pupa(dict):
>         def __getattr__(self, attr):
>             return self[attr]
>         def __setattr__(self, attr, val):
>             self[attr] = val
> 

Maybe I'm missing a step, but this doesnt work.



More information about the Python-list mailing list