from dict to member vars...?
Alex Martelli
aleaxit at yahoo.com
Sat Oct 16 06:17:32 EDT 2004
George Yoshida <ml at dynkin.com> wrote:
> Christopher J. Bottaro wrote:
>
> > Lets say I have a class instance with the following member vars: var1, x,
> > size. Now lets say I have dict with the following keys: var1, x, size.
> > Is there an easy way to *automatically* assign all the values in the dict
> > to corresponding (member) vars of the same name as the dict keys?
> >
> > Thanks for the help.
>
> If class is a new style class, you can do it this way:
>
> instance.__dict__.update(some_dictionary)
You can do this for instances of either classic and newstyle classes,
_except_ newstyle classes which define __slots__ ...
Alex
More information about the Python-list
mailing list