call f(a, *b) with f(*a, **b) ?

bukzor workitharder at gmail.com
Fri May 23 12:12:52 EDT 2008


On May 23, 3:29 am, "inhahe" <inh... at gmail.com> wrote:
> "inhahe" <inh... at gmail.com> wrote in message
>
> news:MKwZj.2749$772.668 at bignews2.bellsouth.net...
>
> > if we assume the constraints are that:
> > 1.he has list, l
> > 2.he has a dictionary, d
> > 3.he wants the function to print the values in the dictionary according to
> > a specific order of their keys as defined by the function, followed by the
> > values of the list
>
> It's also possible (even likely) that he knows outside of the function what
> order he wants the values in, and only used an (incidentally) unordered dict
> called kwargs because he thought that's the only way to pass to those
> parameters.  in which case the function could be left untouched and the he
> would call it like this:
>
> args = [1,2,3]
> f(*args)
> or
> f(*[1,2,3])

The actual application is an option-parser wrapper (it inherits from
optparse.OptionParser). I use the option setting to generate a dict of
the entry-point arguments versus the values specified by the
commandline. This works well. I can then do main(**options).

I was trying to add support for an entrypoint signature like
main(a,b,c,*argv) so that you can have a script with a variable number
of arguments. I can then make a script like: ./script.py a b c ...,
which requires quotes right now. (The actual application is a command
wrapper, where I would like to just add the script to the front of a
command without modification.)

It's easy to get the dictionary I had previously (of arguments versus
values) and a list of extraneous values to be passed to argv, but I
realized that main(*argv, **options) wasn't going to work.

I wish this worked:
>>> def main(a,b,*argv): pass
>>> options['argv'] = argv
>>> main(**options)
TypeError: main() got an unexpected keyword argument 'argv'


Thanks for the help,
--Buck



More information about the Python-list mailing list