This example describes the use of Python callback functions. Share to everyone for your reference. The specific analysis is as follows:
One, Baidu encyclopedia on the callback function explanation:
A callback function is a function called by a function pointer. If you pass the pointer (address) of a function to another function as an argument, when the pointer is used to call the function it points to, we say this is a callback function. A callback function is not invoked directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
Second, what is a callback:
There is always a certain interface between software modules, from the way of invocation, they can be divided into three categories: synchronous call, callback and asynchronous call. A synchronous call is a blocking call, the caller waits for the other party to execute before returning, it is a one-way call; The callback is a two-way invocation pattern, that is, the caller also invokes the interface of the other when the interface is invoked; an asynchronous call is a mechanism similar to a message or event, but it is in the opposite direction. The service of an interface will actively notify the client (that is, the interface that invokes the client) when it receives a message or an event occurs. Callback and asynchronous invocation are very closely related, and we usually use callbacks to register asynchronous messages, and to implement notification of messages by asynchronous invocation. Synchronous calls are the simplest of the three, and callbacks are often the basis for asynchronous invocations, so here we focus on the implementation of callback mechanisms in different software architectures.
三、一个 Small Example:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 |
#call. PY Import called Def callback (): print ' in callback ' Def Main (): #called. Test () Called.test_call (callback) print "in call.py "Main () #called. Py" ' Def Test (): print "in called.py Test ()" "' Def Test_call (p_call): print" in called.py t Est_call () "P_call () joe@joe:~/test/python$ python call.py in called.py Test_call () in callback in call.py joe@joe:~/test/ python$ |
An example of an object-oriented implementation found on the Web:
When you add a callback (Callback) function, the code tends to focus on the implementation of the callback rather than the problem itself. One workaround is to implement a generic base class to resolve the callback requirements, and then implement the method that you bind (Binding) for an event.
The code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the |
|
Here is a class that has two events (EVENT1 and EVENT2) and two processing functions (handler). The first handler handler1 registers the EVENT1, and the second handler Handler2 executes when EVENT1 or EVENT2 occurs (that is, all the events are registered).
The running function (run) in the MyClass main loop, it will send the corresponding event dispatch (Dispatch) out. This (here refers to the dispatch function) returns a function that we can pass to the list of arguments that need to be passed to the function. The end of this function returns a list of all the return values in the list.
Perhaps, the use of metaclass can achieve a more elegant bar.
I hope this article will help you with your Python programming.