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:
d = { 'name': 'Oscar', 'lastName': 'Reyes', 'age':32 }
e = Employee(d)
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
Sure, something like this:
Update
As Brent Nash suggests, you can make this more flexible by allowing keyword arguments as well:
Then you can call it like this:
or like this:
or even like this: