Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing all arguments of a function to another function

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?

like image 970
roopesh Avatar asked Jun 28 '10 23:06

roopesh


People also ask

How do you pass all arguments to a function?

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.

What function passes an argument to another function?

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.

How do you assign a all the arguments to a single variable?

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.


1 Answers

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.

like image 108
Jochen Ritzel Avatar answered Sep 20 '22 11:09

Jochen Ritzel