Insertin **keywords into a class
Ken Seehof
kens at sightreader.com
Tue Mar 20 14:24:36 EST 2001
Use setattr().
You are confusing an attribute with an attribute name (a string).
Each time through the loop you are assigning to the attribute 'kw' rather
than
the attribute whose name is the value of kw.
def __init__(self, *arguments, **keywords):
self.radius = 0.0
for kw in keywords.keys():
print kw, ":", keywords[kw]
setattr(self, kw, keywords[kw])
print "self.radius =", self.radius
or simply...
def __init__(self, *arguments, **keywords):
self.__dict__.update(keywords)
----- Original Message -----
From: "C. Porter Bassett" <porter at et.byu.edu>
Newsgroups: comp.lang.python
To: <kens at sightreader.com>
Cc: <python-list at python.org>
Sent: Tuesday, March 20, 2001 10:53 AM
Subject: Insertin **keywords into a class
> 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)
>
>
> --------------------------------------------------------------------------
> "Pretend like this is a really witty saying." - Anonymous
> --------------------------------------------------------------------------
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list