Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses a function as a function parameter

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report