The meaning of the callback function and an instance of python implementation

  • 2020-06-07 04:41:42
  • OfStack

Due to work needs, I recently learned to use python to parse various files, including xmind, xml, excel, csv and so on.

When I was learning python to parse XML, I saw this passage:

ElementTree(Element tree) ElementTree is like a lightweight DOM with a convenient and friendly API. The code is usable, fast, and consumes less memory. Note: DOM needs to map XML data to the tree in memory, 1 is slower, 2 is more memory consuming, while SAX streaming reads XML file, which is faster and occupies less memory, but requires the user to implement callback function (handler).

At that time, I fell into the old habit and was interested in a noun I didn't know very much -- callback function. After searching for information, I learned 1. I felt I needed to write something to record 1.

The callback function (callback) reads as follows:

A callback function is a function that is called via a function pointer. If you pass a pointer to a function as an argument to another function, it is said to be a callback function when the pointer is used to call the function to which it points. The callback function is not called directly by the implementer of the function, but is called by another 1 when a particular event or condition occurs, in response to that event or condition.

It may be that knowledge is really limited, and it seems confused. And then to find the more easy to understand explanation - like domestic service, homemaking company provides a API: clean the house, but also can provide all kinds of cleaning, such as sweeping the floor, wipe furniture, cleaning the toilet, and so on, we clean the house as a library function, then clean the house or the way it is you, how are you going to clean the will make an appointment and implement the callback function, you make an appointment and perform the service behavior is called to register a callback function. Let me write down an implementation of low that looks pretty easy to understand


# coding=utf-8
#  Please don't mind cleaning the wool so many times 


def clean1(times):
  """
   Just pretend to sweep the floor, this is a way of naming functions, don't learn 
  :param times:  The number of 
  :return: None
  """
  print ' Number of sweeps completed :', str(times)


def clean2(times):
  """
   Silently pretend to wash and smoke range hood 
  :param times:  The number of 
  :return: None
  """
  print ' Number of times the range hood has been washed ', str(times)


def call_clean(times, function_name):
  """
   This is very important, this is the housekeeping company's business system, to what the business must be said here 
   This is the heart of implementing the callback function 
  :param times: The number of 
  :param function_name: The name of the callback function 
  :return: The result of the function called 
  """
  return function_name(times)

if __name__ == '__main__':
  call_clean(100, clean2) #  Give me wash 100 A range hood. All right, it's sick 

Say the last sentence, see this should understand the sentence:

A callback function is a function that is called via a function pointer. If you pass a pointer to a function as an argument to another function, it is said to be a callback function when the pointer is used to call the function to which it points.


Related articles: