SoFunction
Updated on 2024-10-30

Implementation of functions as arguments (callback functions) in python

python with function as argument (callback function)

Purely for my own learning and summarization purposes. The callback function is paired with a tuple and a dictionary. Some of the details that need attention have been marked in the comments.

# With functions as arguments
 
def func_callback(func1,args):
    print("Call function:")
    func1(args)
    print("____________________")
 
def f1(x):
    print("Callback function started:",x)
 
func_callback(f1,100)
 
def func_callback_tuple(func2,*args):
    print("Combining callback functions and tuples:")
    func2(args)# The "*" sign here can be present or omitted.
    print("____________________")
 
def f2(*x):
    print("Callback function startup with tuple argument:")
    print(type(x),x)
 
func_callback_tuple(f2,99,98,97,96)
 
def func_callback_dic(func,**kwargs):
    print("Combining callback functions and dictionaries:")
    func(**kwargs)# Two asterisks to be added, not omitted
    print("____________________")
 
def f3(**x):
    print("Callback function startup with tuple argument:")
    print(type(x),x)
 
func_callback_dic(f3,aa=1,bb=2,cc=3)

The output is:

Argument types for python functions

When calling a function, information can be passed to the function as parameters, which are specified in parentheses after the function name. python has the following four types of parameter passing:

  • Required parameters
  • Keyword parameters
  • default parameter
  • indeterminate length parameter (math.)

I. Mandatory parameters

Required parameters must be passed into the function in the correct order, and must be called with the same number as when declared.

def printme( str ):
   
   print (str)
   return
printme("Hello World!")

The above program must pass a string parameter when calling the printme() function, the responsible program will report an error at runtime.

II. Keyword parameters

Keyword arguments identify the incoming arguments by keyword at the time of the call, and do not necessarily ensure that the order of the arguments is the same as the order of the arguments when the function was declared.

def my_function(child3, child2, child1):
  print("The youngest child is " + child3)
 
my_function(child1 = "Phoebe", child2 = "Jennifer", child3 = "Rory")

III. Default parameters

The default parameter is the value assigned to the formal parameter when the function is declared, and if no value is passed to the parameter when the function is called, then the value assigned at the time of declaration is used by default.

def my_function(name,country = "China"):
  print(name + "is from" + country)
my_function("Zhang San")
my_function("Zhang San","China.")

IV. Indefinite length parameters

Sometimes it may be necessary for a function to be able to handle more arguments than it was originally declared for. These arguments are called indefinite arguments, and the basic syntax is as follows:

Arguments marked with an asterisk * are imported as a tuple, which holds all unnamed variable arguments.

def printinfo( arg1, *vartuple ):
   "Print any incoming parameters."
   print ("Output: ")
   print (arg1)
   print (vartuple)
 
# Call the printinfo function
printinfo( 70, 60, 50 )

Parameters marked with two asterisks ** are imported as dictionaries.

def printinfo( arg1, **vardict ):
   "Print any incoming parameters."
   print ("Output: ")
   print (arg1)
   print (vardict)
 
# Call the printinfo function
printinfo(1, a=2,b=3)

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.