[SOLVED] How to pass function as argument without executing it?

Issue

I have this function:

def a(one, two, the_argument_function):
    if one in two:
        return the_argument_function

my the_argument_function looks something like this:

def b(do_this, do_that):
    print "hi."

Both of the above are imported to a file “main_functions.py” for my ultimate code to look like this:

print function_from_main(package1.a, argument, package2.b(do_this, do_that)

The “if one in two” from “a”function works but “b”function still executes when being passed to “function_from_main” without waiting the check from “a” to see if it actually should execute.

What can I do?

Solution

package2.b(do_this, do_that) is a function call (a function name followed by parenthesis). Instead you should be passing only the function name package2.b the function a

You will also need to modify function a such that function be is called when the condition is satisfied

# function a definition 
def a(one, two, the_argument_function, argument_dict):
    if one in two:
        return the_argument_function(**argument_dict)

def b(do_this, do_that):
    print "hi."

# function call for a
a(one, two, b, {'do_this': some_value, 'do_that': some_other_value}) 

Answered By – shanmuga

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.