Python function as a function argument?

234,001

Solution 1

Can a Python function be an argument of another function?

Yes.

def myfunc(anotherfunc, extraArgs):
    anotherfunc(*extraArgs)

To be more specific ... with various arguments ...

>>> def x(a,b):
...     print "param 1 %s param 2 %s"%(a,b)
...
>>> def y(z,t):
...     z(*t)
...
>>> y(x,("hello","manuel"))
param 1 hello param 2 manuel
>>>

Solution 2

Here's another way using *args (and also optionally), **kwargs:

def a(x, y):
  print x, y

def b(other, function, *args, **kwargs):
  function(*args, **kwargs)
  print other

b('world', a, 'hello', 'dude')

Output

hello dude
world

Note that function, *args, **kwargs have to be in that order and have to be the last arguments to the function calling the function.

Solution 3

Functions in Python are first-class objects. But your function definition is a bit off.

def myfunc(anotherfunc, extraArgs, extraKwArgs):
  return anotherfunc(*extraArgs, **extraKwArgs)

Solution 4

Sure, that is why python implements the following methods where the first parameter is a function:

  • map(function, iterable, ...) - Apply function to every item of iterable and return a list of the results.
  • filter(function, iterable) - Construct a list from those elements of iterable for which function returns true.
  • reduce(function, iterable[,initializer]) - Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
  • lambdas

Solution 5

Function inside function: we can use the function as parameter too..

In other words, we can say an output of a function is also a reference for an object, see below how the output of inner function is referencing to the outside function like below..

def out_func(a):

  def in_func(b):
       print(a + b + b + 3)
  return in_func

obj = out_func(1)
print(obj(5))

the result will be.. 14

Hope this helps.

Share:
234,001
a7664
Author by

a7664

Updated on January 03, 2020

Comments

  • a7664
    a7664 over 4 years

    Can a Python function be an argument of another function?

    Say:

    def myfunc(anotherfunc, extraArgs):
        # run anotherfunc and also pass the values from extraArgs to it
        pass
    

    So this is basically two questions:

    1. Is it allowed at all?
    2. And if it is, how do I use the function inside the other function? Would I need to use exec(), eval() or something like that? Never needed to mess with them.

    BTW, extraArgs is a list/tuple of anotherfunc's arguments.

  • Aysennoussi
    Aysennoussi about 9 years
    can extraArgs be a function as well ? if so how do you call it ?
  • Manuel Salvadores
    Manuel Salvadores about 9 years
    @sekai Yes, extraArgs can be a function too.
  • Pipo
    Pipo over 6 years
    More infos about *args & **kwargs can be found here pythontips.com/2013/08/04/args-and-kwargs-in-python-explaine‌​d
  • HMD
    HMD about 6 years
    Please consider adding some explanation to your answer.
  • 4dc0
    4dc0 about 5 years
    where is this documented?
  • Bob Bobster
    Bob Bobster almost 5 years
    You are not passing function as an argument here, instead the return value.
  • Karl Knechtel
    Karl Knechtel about 2 years
    @4dc0 I don't think it's directly documented; it's just a natural consequence of how Python's data model works. Everything that can be bound to a name is an object; objects can be passed to functions; functions can be bound to a name (that's what happens when you use def); therefore, functions can be passed to functions. That said, you may be interested in docs.python.org/3.11/howto/functional.html, which shows elementary techniques that make use of this reality, and shows off some of the standard library goodies designed to capitalize on it.
  • Karl Knechtel
    Karl Knechtel about 2 years
    See also: docs.python.org/3.11/howto/functional.html. The builtins also include sort, min and max which accept a key keyword argument that is expected to be a callable.
  • Karl Knechtel
    Karl Knechtel about 2 years