Set attributes from dictionary in python

Posted by Oscar Reyes on Stack Overflow See other posts from Stack Overflow or by Oscar Reyes
Published on 2010-03-17T21:54:59Z Indexed on 2010/03/17 22:01 UTC
Read the original article Hit count: 630

Is it possible to create an object from a dictionary in python in such a way that each key is an attribute of that object?

Something like this:

 dict = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }

 e = Employee( dict ) 
 print e.name # Oscar 
 print e.age + 10 # 42 

I think it would be pretty much the inverse of this question: Python dictionary from an object's fields

© Stack Overflow or respective owner

Related posts about python

Related posts about initialization