[Tutor] Initialize Class Variables by Dictionary ...
Kent Johnson
kent37 at tds.net
Sun Aug 30 03:29:07 CEST 2009
On Sat, Aug 29, 2009 at 6:30 PM, Damon Timm<damontimm at gmail.com> wrote:
> Hey! I am a newbie too but it works for me:
>
>>>> class Test(object):
> ... def __init__(self,dict):
> ... for key in dict:
> ... self.__setattr__(key,dict[key])
Use the setattr() builtin:
setattr(self, key, dict[key]}
or just
def __init__(self, d):
self.__dict__.update(d)
BTW don't use dict as the name of a variable, it shadows the built-in
of the same name.
Kent
More information about the Tutor
mailing list