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

bukzor workitharder at gmail.com
Fri May 23 02:26:59 EDT 2008


On May 22, 5:29 pm, "inhahe" <inh... at gmail.com> wrote:
> "bukzor" <workithar... at gmail.com> wrote in message
>
> news:55bc8846-107e-461a-89de-25c83cbcfa27 at 2g2000hsn.googlegroups.com...
>
> > This question seems easy but I can't figure it out.
> > Lets say there's a function:
>
> > def f(a, *args):
> >    print a
> >    for b in args: print b
>
> > and elsewhere in your program you have a list and a dict like this:
> > args = [2, 3]
> > kwargs = {'a':1}
>
> > I'd like to get f() to print something like the following, but I can't
> > figure out how.
> > 1
> > 2
>
> I think there's no 'standard' way to do this. but:
>
> import inspect
> f(*map(kwargs.get, inspect.getargspec(f)[0])+args)
>
> i don't know if it works because i'm afraid to try it.  if it doesn't the
> solution is something similar to that.

That does, in fact work. Thanks! I'm a little sad that there's no
builtin way to do it, owell.

>>> def f(a, *args):
...     print a
...     for b in args: print b
...
>>> import inspect
>>> a = [2,3]
>>> b = {'a':1}
>>> inspect.getargspec(f)
(['a'], 'args', None, None)
>>> map(b.get, inspect.getargspec(f)[0])
[1]
>>> map(b.get, inspect.getargspec(f)[0]) + a
[1, 2, 3]
>>> f(*map(b.get, inspect.getargspec(f)[0]) + a)
1
2
3



More information about the Python-list mailing list