converting dict to object
Carl D. Roth
roth at ursus.net
Sat Dec 2 12:56:05 EST 2006
On Fri, 01 Dec 2006 17:48:40 -0800, rieh25 wrote:
> If I have a dictionary such as:
>
> d = {'a' : 1, 'b' : 2}
>
> is there a way to convert it into an object o, such as:
>
> o.a = 1
> o.b = 2
Rather, the question could be asked the other way around: how can you
convert that object into a dict?
The attributes in 'o' are part of its '__dict__' attribute, which is a
dictionary. That is,
o.a = 1
is equivalent to
o.__dict__['a'] = 1
If you already have a dictionary 'd', you can overlay it onto the object's
attributes with
o.__dict__.update(d)
Carl
More information about the Python-list
mailing list