views:

88

answers:

2

I want to pass all the arguments passed to a function(func1) as arguments to another function(func2) inside func1 This can be done with *args, *kwargs in the called func1 and passing them down to func2, but is there another way,

Orignially

def func1(*args, **kwargs):
    func2(*args, **kwargs)

but if my func1 signature is

def func1(a=1, b=2, c=3):

how do I send them all to func2, without using

def func1(a=1, b=2, c=3):
    func2(a, b, c)

Is there a way as in javascript callee.arguments

+4  A: 

Explicit is better than implicit but if you really don't want to type a few characters:

def func1(a=1, b=2, c=3):
    func2(**locals())

locals() are all local variables, so you can't set any extra vars before calling func2 or they will get passed too.

THC4k
This will have the flaw that if you've created local variables before calling `func2`, then they will also be passed to `func2`.
Ned Batchelder
maybe you could copy locals right at the beginning of the function?
Bwmat
ya, copying is fine i guess.
roopesh
Now I remember reading about `locals`. I was thinking too much about `vars()` and couldn't remember `locals()`. Should have read the documents. I guess vars() can be used too.
roopesh
+2  A: 

Provided that the arguments to func1 are only keyword arguments, you could do this:

def func1(a=1, b=2, c=3):
    func2(**locals())
Kip Streithorst