Python callback function
"A callback function is a function called using a function pointer.
If you pass the pointer (address) of a function as a parameter to another function, when the pointer is used to call the function to which it points, we will say this is a callback function ."
-- The explanation of a callback function that is extracted from the Internet. Okay, it's better.
Let's take an example:
The school has to perform Entry and Exit Control, telling the guard to report the pets and cars (this is the callback function registration ), then the Administrator processes the callback function based on the report from the guard ).
Import OS, sys
Find = {
'Type ':'',
'Color ':'',
'SIZE ':''
} # Define the Report Content
Def CallFun (cmd, Find): # define the callback function. handle various callback situations here.
If cmd = 'type ':
If Find ['type'] = 'Dog' or Find ['type'] = 'cat ':
Print 'A Pet :'
Else:
Print 'a Transport :'
Elif cmd = 'print ':
Print Find
Else:
Print 'error'
Def GiveInfo (I): # This section is filled in and can be ignored.
Type0 = ['Dog', 'cat']
Type1 = ['cart', 'truck ']
Color0 = ['black', 'white', 'pink ']
Size0 = ['Big ', 'middle', 'small']
T0 = I % 2
If t0 = 0:
Find ['type'] = type0 [I % 2]
Else:
Find ['type'] = type1 [I % 2]
Find ['color'] = color0 [I % 3]
Find ['SIZE'] = size0 [I % 3]
Def FindObj (num, cmd, CallBackFun): # Find the target and start the callback function.
GiveInfo (num) # Information provided by the guard
CallBackFun (cmd, Find) # Start the callback function
If _ name _ = '_ main __':
Cmds = ['type', 'print ', 'try']
For I in range (): # define 10 reports
Print '---------- % d -------------' % I
FindObj (I, cmds [I % 3], CallFun) # register the callback function here (the process of notifying the guard)
Callback facilitates module decoupling.