Automatic binding of **kwargs to variables
chris.atlee at gmail.com
chris.atlee at gmail.com
Sat Oct 29 14:01:02 EDT 2005
Mike Meyer wrote:
[snip]
> for name, value in kwargs.items():
> if name in ('a', 'list', 'of', 'valid', 'keywords'):
> exec "%s = %s" % (name, value)
> else:
> raise ValueError, "Unrecognized keyword " + name
>
> Others will probably tell you that you really shouldn't be using exec.
What about using setattr?
for name, value in kwargs.items():
if name in ('a', 'list', 'of', 'valid', 'keywords'):
setattr(self, name, value)
else:
raise ValueError, "Unrecognized keyword " + name
I'd probably turn the list of valid keywords into another dictionary to
make it easy to specify default values for all the parameters as well.
Cheers,
Chris
More information about the Python-list
mailing list