[Tutor] Turning kwargs into scalars

bob bgailer at alum.rpi.edu
Tue Nov 1 17:17:14 CET 2005

At 08:52 PM 10/31/2005, Steve Bergman wrote:
>Say I have a function:
>def f(self, **kwargs) :

FWIW you don't have a function at this point. You have a def statement 
which must be followed by at least one indented statement, which in turn 
must be executed. Then you have a function.

>and I want to take the key/value pairs and create a set of variables with 
>the names of the keys.
>For example, if I say:
>f(x=5, y=2)
>I want to create local variables 'x' and 'y' in the function, with values 
>of 5 and 2 respectively.
>How could I do this?

This is an FAQ. It can be done. However there are better ways to accomplish 
the same thing. Namely use the dictionary kwargs and look things up by key.

Would you give an example of what you'd do with variables x and y after 
creating them? 

More information about the Tutor mailing list