Automatic binding of **kwargs to variables
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sun Oct 30 07:42:52 EST 2005
On Fri, 28 Oct 2005 15:49:12 -0700, lbolognini at gmail.com wrote:
> I have a very long list of parameters coming from a web form to my
> method foo(self, **kwargs)
>
> I would like to avoid manually binding the variables to the values
> coming through the **kwargs dictionary,
That's easy: Just Don't Do It.
Instead of some horribly contorted code converting key,item in kwargs into
self.key = item, just do the simple, obvious thing:
self.kwargs = kwargs
and then access the values with self.kwargs[key].
> just to keep the code cleaner,
> I'd like to bind them automatically
If you are worried about excessive use of quotation marks, you can always
define some constants to use:
NAME = 'name'
EMAIL = 'email'
NUM = 'num'
if self.kwargs[NAME]is None:
self.kwargs[NAME] = "Anonymous"
The benefit of this is that when you change your web form to use "number"
instead of "num", you only have to change your code in one place.
--
Steven
More information about the Python-list
mailing list