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?
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
?
Within any function, you can use the arguments variable to get an array-like list of all of the arguments passed into the function. You don't need to define it ahead of time. It's a native JavaScript object. You can access specific arguments by calling their index.
Higher Order Functions Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.
Assigning the arguments to a regular variable (as in args="$@" ) mashes all the arguments together like "$*" does. If you want to store the arguments in a variable, use an array with args=("$@") (the parentheses make it an array), and then reference them as e.g. "${args[0]}" etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With