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

bukzor workitharder at gmail.com
Fri May 23 12:36:44 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])

You're actually pretty dead-on here. I preferred the keyword approach
because printing it showed an explicit assignment of arguments, but it
looks like I'll have to use a list if there's an *argv argument, since
there's no way to assign it with keywords, and no (working) way to use
them together.

def f(a, *args): print a, args
args = [2, 3]
kwargs = {'a':1}

f(*args, **kwargs)
TypeError: f() got multiple values for keyword argument 'a'

kwargs['args'] = args
f(**kwargs)
TypeError: f() got an unexpected keyword argument 'args'



More information about the Python-list mailing list