object attributes from a dictionary

Darren Dale dd55 at cornell.edu
Tue Jul 13 18:24:49 EDT 2004


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.)



More information about the Python-list mailing list