object attributes from a dictionary

Lonnie Princehouse finite.automaton at gmail.com
Wed Jul 14 22:39:29 CEST 2004


class pupa:
  def __init__(self, **kwargs):
    self.__dict__.update(kwargs)




Darren Dale <dd55 at cornell.edu> wrote in message news:<cd1nfj$15$1 at news01.cit.cornell.edu>...
> 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