[Tutor] dynamic argument lists

Kent Johnson kent37 at tds.net
Fri Aug 29 18:20:18 CEST 2008


On Fri, Aug 29, 2008 at 9:49 AM, eShopping
<etrade.griffiths at dsl.pipex.com> wrote:
> Hi
>
> I have a GUI program that extracts some information from the user as
> strings, and I then want to use the strings to form an argument list to
> another function.  Hopefully the following code gives some flavour:
>
> def myfunc(**kwargs):
>    while kwargs:
>        name, value = kwargs.popitem()
>        print name, value
>
> myfunc(a=1, b=2, c=3, d=4)
> arg_str = "a=1, b=2, c=3, d=4"
> myfunc(arg_str)
>
> ARG_STR will be built up from the data extracted from the GUI.  I get this
> error
>
> TypeError: myfunc() takes exactly 0 arguments (1 given)
>
> I understand that ARG_STR is a string and that MYFUNC is expecting something
> else ,,, but not sure what it is.  I have tried various dictionary
> configurations such as
>
> arg1 = ["a","b","c","d"]
> arg2 = [1,2,3,4]
> arg3 = dict(zip(arg1,arg2))
> myfunc(arg3)

If you want to pass a dict containing the keywords, rather than actual
keywords, the syntax is
myfunc(**arg3)

It parallels the syntax used to define myfunc().

Kent


More information about the Tutor mailing list