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

What does the closure in python mean?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "what does closure mean in python". In daily operation, I believe many people have doubts about what closure means in python. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the question of "what does closure mean in python"! Next, please follow the small series to learn together!

Definition of closure

A closure is a function defined inside a function.

logic of closure

After understanding the logic of closures, it is easy to write closures with a certain Python foundation. Let's look at the logic of closure through an example.

def discount(x): if x1: return None def count(prince, number): result = prince * number pay = result * x print(f'total price is {result} yuan, actually paid {pay} yuan') return countdiscount(0.8)(2.88, 100)out: Total price is 288.0 yuan, actually paid 230.4 yuan

The above code is often encountered when selling goods, discount function is the outer function, used to detect whether the discount number is reasonable. Count is an inner function that calculates the total amount and the actual amount paid.

When the python interpreter encounters def discount, it adds a discount object to the global namespace whose value points to the code block inside the discount function. The discount function is not running at this point.

Start the function discount(0.8) when discount(0.8)(2.88, 100) is encountered, and add a count object to the local namespace of discount when def count is encountered, whose value points to the code block inside the count function. Return count returns the count object to discount(0.8)(2.88,100). At this point the discount(0.8) part has been run, and the next run is count(2.88,100). After the inner code block of count(2.88,100) runs, the entire closure function runs.

)

Extension of closure

Closures can be extended to make them easier to use in the following ways:

def discount(x): if x

< 0.5 or x >

1: print ('Discount figures are unreasonable. ') return def count(prince, number): result = prince * number pay = result * x print(f'total price is {result} yuan, actually paid {pay} yuan') return countgoldcard = discount(0.7)silvercard = discount(0.9) commoncard = discount(1)goldcard(2.88, 100)silvercard(2.88, 100) commoncard (2.88,100)print(id(goldcard)) print (id(silvercard)) print (id(commoncard))out: total price is 288.0 yuan, actually paid 201.6 yuan total price is 288.0 yuan, actually paid 259.2 yuan total price is 288.0 yuan, actually paid 2868027040961987081368361987081368192

Assigning the outer function plus parameters to another variable is equivalent to defining a new function and improving the code reuse rate. Although the above three functions goldcard, silvercard and commoncard all point to the discount function, because their parameters are different, they are actually three independent objects, and you can see that their ids are different.

At this point, the study of "what closure means in python" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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

Internet Technology

Wechat

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

12
Report