Passing functions with arguments to another function in Python?


Question

Is it possible to pass functions with arguments to another function in Python?

Say for something like:

def perform(function):
    return function()

But the functions to be passed will have arguments like:

action1()
action2(p)
action3(p,r)
1
182
5/27/2018 7:00:54 PM

Accepted Answer

Do you mean this?

def perform( fun, *args ):
    fun( *args )

def action1( args ):
    something

def action2( args ):
    something

perform( action1 )
perform( action2, p )
perform( action3, p, r )
267
4/29/2009 6:41:05 PM

This is what lambda is for:

def Perform(f):
    f()

Perform(lambda: Action1())
Perform(lambda: Action2(p))
Perform(lambda: Action3(p, r))

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon