Insertin **keywords into a class

John J. Lee phrxy at csv.warwick.ac.uk
Tue Mar 20 17:51:03 EST 2001


On Tue, 20 Mar 2001, C. Porter Bassett wrote:

> cCan you please tell me why the following code snippet doesn't update the
> value of radius?
>
>
> class myClass:
>    def __init__(self, *arguments, **keywords):
>       self.radius = 0.0
>       for kw in keywords.keys():
>          print kw, ":", keywords[kw]
>          self.kw = keywords[kw]
>       print "self.radius =", self.radius
>
>
> b = myClass(radius = 1.0)

You're setting self.kw to 1.0 when you do

self.kw = keywords[kw]

You want to set self.radius to 1.0, and you can do that with

self.__dict__[kw] = keywords[kw]


John




More information about the Python-list mailing list