Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses a function as a function parameter

Shulou Source: shulou.com Published: 2022-06-01 02:31:50 09月21日 Update

This article will explain in detail how python uses functions as function parameters. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use a function as a function parameter

Sometimes you need to define a function, most of the calculation logic of the function can be determined, but some processing logic can not be determined for the time being, which means that some program code needs to be changed dynamically. If you want to dynamically introduce these codes when calling the function, then you need to define the function parameters in the function, so that you can pass in different functions as parameters when calling the function, thus changing the code dynamically.

Python supports the use of function parameters like other parameters, such as the following programs:

# defines the formal parameter of the function type, where fn is a function def map (data, fn): result = [] # traverses each element in the data list And use the fn function to calculate each element # and then take the calculation result as the element of the new array for e in data: result.append (fn (e)) return result# defines a square function def square (n): return n * n # defines a cube function def cube (n): return n * n # defines an order of calculation Multiplication function def factorial (n): result = 1 for index in range (2) N + 1): result * = index return resultdata = [3,4,9,5,8] print ("original data:", data) # the following program code calls the map () function three times Pass in a different function print ("calculating the square of array elements") print (map (data, square)) print ("calculating the cube of array elements") print (map (data, cube)) print ("calculating the factorial of array elements") print (map (data, factorial))

A map () function is defined in the above program, and the second parameter of this function is a parameter of a function type, which means that a function can be dynamically passed in each time the function is called. With the change of the actual incoming function, part of the calculation code in the map () function can be changed dynamically.

The next three lines of bold code call the map () function three times, which in turn passes in the square, cube, and factorial functions as arguments, so that the actual execution code is different each time the map () function is called.

After compiling and running the above program, you can see the following output:

Original data: [3,4,9,5,8] to calculate the square of array elements [9,16,81,25,64] to calculate the cube [27,64,729,125,512] to calculate the factorial of array elements [6,24,120,40320]. This is the end of the article on "how python uses functions as function parameters". I hope the above content can be helpful to you. So that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Tags: Functions elements code arrays parameters formal parameters dynamics programs articles factorials different actual data more types results logic good practical next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn macOS Xiaomi Shulou Information