Use callback function in Python

What is a callback function?

A callback function is a function that is specified in advance so that it will be executed during the execution of the called function in the program. http://e-words.jp/w/%E3%82%B3%E3%83%BC%E3%83%AB%E3%83%90%E3%83%83%E3%82%AF%E9%96%A2%E6%95%B0.html

It is also sometimes described as a function passed as a function argument.

how to use

It is a linked article http://qiita.com/pocket8137/items/df5afba90b51e90587a5 First, define a function that calls the callback function.

def handler(func,*args):
    return func(*args)

The first argument is a function pointer and the second argument is a variable argument. The argument of the callback function is received as the second argument.

And define the callback function

def say_hello(name):
    print("Hello!!")
    print(name)

Call. By the way, in Python, the function pointer is pointed to by not adding () to the function name.

if __name__ == "__main__":
    callback = say_hello
    #say to callback_Stores hello object ID
  handler(callback, "moroku0519")

Execution result

   Hello!!
   moroku0519

What did you use it for

In my case, there were occasions when I wanted to separate the functions used. Therefore, I defined a dictionary that stores the object ID.

def handler(func, *args):
    return func(*args)

def function1(hoge):
    return hoge

def function2(hoge, huga):
    return hoge, huga

func_dic = {"func1": function1, "func2": function2}
result1 = handler(func_dic["func1"], hoge)
result2, result3 = handler(func_dic["func2"], hoge, huga)

Recommended Posts

Use callback function in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Let's use def in python
Use let expression in Python
Create a function in Python
Use parameter store in Python
ntile (decile) function in python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Nonlinear function modeling in Python
Draw implicit function in python
Immediate function in python (lie)
python function ①
[Python] function
python function ②
[python] Callback function (pass function as argument)
Function argument type definition in python
Use fabric as is in python (fabric3)
How to use SQLite in Python
Write AWS Lambda function in Python
Measure function execution time in Python
Use rospy with virtualenv in Python3
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Function synthesis and application in Python
Use OpenCV with Python 3 in Window
How to use python zip function
To return char * in a callback function using ctypes in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Use print in a Python2 lambda expression
python enumerate function
Meta-analysis in Python
Precautions when pickling a function in python
Unittest in python
Easily use your own functions in Python
OR the List in Python (zip function)
Python> function> Closure
Epoch in Python
[python] How to use __command__, function explanation
Discord in Python
[Python] Generator function
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python