Insertin **keywords into a class

Ben Hutchings ben.hutchings at roundpoint.com
Tue Mar 20 15:31:52 EST 2001


"C. Porter Bassett" <porter at et.byu.edu> writes:

> cCan you please tell me why the following code snippet doesn't update the
> value of radius?

Yes...

> class myClass:
>    def __init__(self, *arguments, **keywords):
>       self.radius = 0.0
>       for kw in keywords.keys():
>          print kw, ":", keywords[kw]
>          self.kw = keywords[kw]

This binds/re-binds self.kw, not the attribute of self named by kw.  I
think you can accomplish what you want by putting self.__dict__[kw] on
the left hand side of the assignment.

>       print "self.radius =", self.radius
> 
> 
> b = myClass(radius = 1.0)

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list