Implementation Analysis of python callback functions and callback methods, and python callback

Source: Internet
Author: User

Implementation Analysis of python callback functions and callback methods, and python callback

Callback and event-driven
Callback functions have an important significance: they are event-driven.
Let's imagine a scenario where we trigger an event, such as a click event.
You only need to bind one or more processing events to this click event, that is, the callback function.
We can call the corresponding method after the click event is triggered (after the event) to handle the problem.
For example

Common callback

# Use the callback def click (callback): eval (callback) () # eval () to parse strings into executable code def handle: print 'call this function after clicking the event to process it. For example, in the pop-up box alert () 'if (_ name _ = '_ main __'): fun1 ('fun2 ')

The above scenario is obviously a scenario where we use browsers on a daily basis:
When we click a button, a warning box or confirmation box may pop up, which can be understood
After an event is triggered, the handler function is called. The function prompts a warning box or confirmation box.
Event-driven: click event sending to drive the trigger of the handle Handler
Well, will this be clearer?

Class Using callback
Since python is an object-oriented orientation from the very beginning, as a python programmer, it may face the following scenarios:
We create an instance, pass in the data to this instance, and finally get the result we want, for example
An object named getCsdnTitle and a url (the url of A csdn blog). We hope to get the text content of this article,
Do we first get the html content corresponding to this url through an http request, and name this method fetch_url ()
Then we need to process html, such as filtering, regular expression matching, string processing, and get the desired text content. Name this method
Get_content ()
In this way, the object needs to pass through the fetch_url and get_content methods to get the final result we need, and the two are obvious.
The difference in order can be understood as calling the get_content method after fetch_url is called. You may say that this is a lot of trouble,
In fact, you need to know that this execution chain may be very long in actual development. Through callback, we can specify what we will do after a specific step is executed.
Operation, which is logical and understandable.

The following example shows how to implement callback in a class.

# Use the callback method in the class

# Use the callback def click (callback): eval (callback) () # eval () to parse strings into executable code def handle: print 'call this function after clicking the event to process it. For example, in the pop-up box alert () 'if (_ name _ = '_ main __'): fun1 ('fun2 ')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.