Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 685
Dan Von Avatar asked Jul 01 '26 18:07

Dan Von


1 Answers

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}) 
like image 58
shanmuga Avatar answered Jul 03 '26 07:07

shanmuga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!