views:

64

answers:

1

Hi, is to possible to "pack" arguments in python? I have the following functions in the library, that I can't change (simplified):

def g(a,b=2):
    print a,b

def f(arg):
    g(arg)

I can do

o={'a':10,'b':20}
g(**o)
10 20

but can I/how do I pass this through f?

That's what I don't want:

f(**o)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got an unexpected keyword argument 'a'

f(o)
{'a': 10, 'b': 20} 2
+1  A: 

f has to accept arbitrary (positional and) keyword arguments:

def f(*args, **kwargs):
    g(*args, **kwargs)

If you don't want f to accept positional arguments, leave out the *args part.

Philipp
The problem here is, that I cannot change f or g, it's not mine :) Of course I could file a bug.
Sorry, I misinterpreted your question in that respect. Then I think you have no other choice than reimplementing `f` or requesting a change in the library.
Philipp
As it stands, `f` provides no way to pass a value to `b` in `g`, so you should file a bug
gnibbler