[Tutor] using **kwargs in __init__() as attributes
Dear Tutors, I would like to make a new class instance, where the intance attributes coming from the kwargs hash. class ADUser: def __init__(self, **kwargs): for key in kwargs.keys(): self.key = kwargs[k] a = ADUser(name='papa') It isn't working :( Yours sincerely, __ Janos Juhasz ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using **kwargs in __init__() as attributes
János Juhász wrote: Dear Tutors, I would like to make a new class instance, where the intance attributes coming from the kwargs hash. class ADUser: def __init__(self, **kwargs): for key in kwargs.keys(): self.key = kwargs[k] This sets the 'key' attribute of self to the value of the keyword. You want to use the value of of key as the attribute name. You can do this with setattr(): setattr(self, key, kwargs[k]) You can also update all the kwargs at once with self.__dict__.update(kwargs) It isn't working :( A more specific description of the problem is often helpful; for best results copy and paste the exact error message, including the traceback, into your email. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using **kwargs in __init__() as attributes
On Mon, Oct 01, 2007 at 04:51:53PM +0200, J?nos Juh?sz wrote: Dear Tutors, I would like to make a new class instance, where the intance attributes coming from the kwargs hash. class ADUser: def __init__(self, **kwargs): for key in kwargs.keys(): self.key = kwargs[k] You are asking a more sophisticated form of a question that has been asked on this list several times before: How do I create names in a namespace from string values? It's likely that you do not really want (or need) to do this. Try the following instead: def __init__(self, **kwargs): self.values = kwargs Then access the values with: x = self.values['name'] or, safer: x = self.values.get('name', 'no_name') If you are determined to create names in a namespace, on the fly, look at the exec command (http://docs.python.org/ref/exec.html). But, that's a dangerous way to code. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using **kwargs in __init__() as attributes
János Juhász [EMAIL PROTECTED] wrote class ADUser: def __init__(self, **kwargs): for key in kwargs.keys(): self.key = kwargs[k] a = ADUser(name='papa') It isn't working :( Try using setattr instead of self.key assignment. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using **kwargs in __init__() as attributes
János Juhász wrote: Dear Tutors, I would like to make a new class instance, where the intance attributes coming from the kwargs hash. class ADUser: def __init__(self, **kwargs): for key in kwargs.keys(): self.key = kwargs[k] a = ADUser(name='papa') Your snippet above will set the attribute key to kwargs[k] (it will overwrite the values for the previous keys so you won't get anywhere). You need to use setattr to do this since the names of the attributes are available as strings. Here's an example. class Foo(object): ... def __init__(self,**d): ...for i in d: ... setattr(self,i,d[i]) ... x= Foo(name=Papa) x.name 'Papa' -- ~noufal http://nibrahim.net.in/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor