Andreas Sandberg Andreas Sandberg - 3 months ago 10
Python Question

Python function in function

def example(function):
if input() == "Hello there!":
#at this point I want to call the function entered in the tuples


an example of what I mean:

def example(function):
if input() == "Hello there!":
#do the function here

def Printer(What_to_print):
print(What_to_print + "Just an example")


example(Printer)


Is this possibe and are there drawbacks in doing this?

Answer

Yes. It is possible.

def example(function):
    if input() == "Hello there!":
        function("Hello there!")  # invoke it!

Actually you can pass def functions and lambda functions as parameters and invoke them by () syntax.

Comments