Passing all arguments of a function to another function
Solution 1
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.
Solution 2
Provided that the arguments to func1 are only keyword arguments, you could do this:
def func1(a=1, b=2, c=3):
func2(**locals())
Solution 3
As others have said, using locals()
might cause you to pass on more variables than intended, if func1()
creates new variables before calling func2()
.
This is can be circumvented by calling locals()
as the first thing, like so:
def func1(a=1, b=2,c=3):
par = locals()
d = par["a"] + par["b"]
func2(**par)
Related videos on Youtube
roopesh
I just love programming, and want to contribute back to the community.
Updated on November 21, 2020Comments
-
roopesh over 3 years
I want to pass all the arguments passed to a function(
func1
) as arguments to another function(func2
) insidefunc1
This can be done with*args, *kwargs
in the calledfunc1
and passing them down tofunc2
, but is there another way?Originally
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
?-
Mark Elliot about 14 yearspossibly related: stackoverflow.com/questions/1136673/…
-
jcao219 about 14 yearsI don't see how this could be useful...
-
roopesh about 14 yearsit's not all that useful, but sometimes I write a function with lot of args, and and bored of copying them over and over.
-
roopesh about 14 years@Mark no, not related, but thanks anyway.
-
b0fh about 11 years@jcao219 this is useful when writing generic decorators that do not care about function args
-
Marc almost 8 years@jcao219 - or if you're extracting common functionality to another function, and you don't what to change the existing external calling interface
-
-
yantrab about 14 yearsThis will have the flaw that if you've created local variables before calling
func2
, then they will also be passed tofunc2
. -
Bwmat about 14 yearsmaybe you could copy locals right at the beginning of the function?
-
roopesh about 14 yearsNow I remember reading about
locals
. I was thinking too much aboutvars()
and couldn't rememberlocals()
. Should have read the documents. I guess vars() can be used too. -
Bhaskar almost 10 yearsThis would not work for class functions as it will get multiple values for self
-
orome about 8 years@Bhaskar: Is there any way around that?
-
kuzzooroo almost 8 years@Bhaskar, can you elaborate on the problem? I'm only seeing one copy of
self
, even when using multiple inheritance. What's the "this" that won't work, usinglocals()
at all, or trying to save a copy of the result as a way to have a record after a local variable has been created? -
Tom Swirly over 7 yearsOuch, I hate this one. The reason is that
locals()
is the wrong concept - you want to pass "all the parameters" not "all the local variables". All sorts of innocent changes - like adding a local variable before this line, or moving a function to be a method or vice versa - can break this code. More, the reader has to scratch their head to figure it out. -
Nex about 5 yearsIs there a lean general solution to pass mixed parameters from arguments and keyword arguments?
-
Hacker over 2 yearsWhy only keyword arguments?