Python callback function

Source: Internet
Author: User

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.

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.