I'm trying to call a function inside another function in python, but can't find the right syntax. What I want to do is something like this:
def wrapper(func, args):
func(args)
def func1(x):
print(x)
def func2(x, y, z):
return x+y+z
wrapper(func1, [x])
wrapper(func2, [x, y, z])
In this case first call will work, and second won't. What I want to modify is the wrapper function and not the called functions.
A small addition to previous answers, since I couldn't find a solution for a problem, which is not worth opening a new question, but led me here.
Here is a small code snippet, which combines
lists
,zip()
and*args
, to provide a wrapper that can deal with an unknown amount of functions with an unknown amount of arguments.Keep in mind, that
zip()
does not provide a safety check for lists of unequal length, see zip iterators asserting for equal length in python.The simpliest way to wrap a function
... is to manually write a wrapper that would call func() inside itself:
In Python function is an object, so you can pass it's name as an argument of another function and return it. You can also write a wrapper generator for any function anyFunc():
Please also note that in Python when you don't know or don't want to name all the arguments of a function, you can refer to a tuple of arguments, which is denoted by its name, preceded by an asterisk in the parentheses after the function name:
For example you can define a function that would take any number of arguments:
Python provides for even further manipulation on function arguments. You can allow a function to take keyword arguments. Within the function body the keyword arguments are held in a dictionary. In the parentheses after the function name this dictionary is denoted by two asterisks followed by the name of the dictionary:
A similar example that prints the keyword arguments dictionary:
To expand a little on the other answers:
In the line:
The * next to
args
means "take the rest of the parameters given and put them in a list calledargs
".In the line:
The * next to
args
here means "take this list called args and 'unwrap' it into the rest of the parameters.So you can do the following:
In
wrapper2
, the list is passed explicitly, but in both wrappersargs
contains the list[1,2,3]
.You can use *args and **kwargs syntax for variable length arguments.
What do *args and **kwargs mean?
And from the official python tutorial
http://docs.python.org/dev/tutorial/controlflow.html#more-on-defining-functions
You need to use arguments unpacking..
The literal answer to your question (to do exactly what you asked, changing only the wrapper, not the functions or the function calls) is simply to alter the line
to read
This tells Python to take the list given (in this case,
args
) and pass its contents to the function as positional arguments.This trick works on both "sides" of the function call, so a function defined like this:
would be able to accept as many positional arguments as you throw at it, and place them all into a list called
args
.I hope this helps to clarify things a little. Note that this is all possible with dicts/keyword arguments as well, using
**
instead of*
.