object attributes from a dictionary

Scott David Daniels Scott.Daniels at Acm.Org
Thu Jul 15 02:04:09 CEST 2004


Darren Dale wrote (in response to John Lenton):

> I see. I wasnt clear when I asked my original question. A dictionary is 
> already an object. I wanted to take a dictionary and by creating a new 
> object, turn the key/value pairs into object attributes.
> 

Is this what you were looking for?:
     ...
     def __init__(self, initDict, ...):
         for key, val in initDict.iteritems():
              setattr(self, key, val)

or were you looking to explicitly play with magic?:

     ... # probably only on classic classes:
     def __init__(self, initDict, ...):
         self.__dict__.update(initDict)

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list