In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use closures in Python, which is very detailed and has certain reference value. Friends who are interested must read it!
1. The concept of closure
The concept of closure in a function is simply that a function definition refers to a variable defined outside the function, and the function can be executed outside its definition environment. Such a function is called a closure. In fact, closures can be seen as a broader concept of function. Because it is no longer a function defined in the traditional sense.
The concept of closures exists not only in Python, but in almost any programming language.
2. Closure condition
Conditions of closure:
Internal functions are defined in external functions
The external function has a return value
The return value is: internal function
The internal function also refers to the variables of the external function.
The format is as follows:
Def external function ():... Def internal function ():... Return internal function
Sample code:
Def func (): a = 100def inner_func (): B = 200print (a, b) return inner_funcx = func () print (x) # x () # 100 20cm so that the inner_func function can be called directly. If return does not return an internal function, there will not be any output. 3. Closure completion counting effect
You can also use closures to achieve the effect of a counter
Def generate_count (): container = [0] def add_one (): container [0] + = 1 print (f "this is the {container [0]} call") return add_onecount = generate_count () count () # this is the first call to count () # this is the second call to count () # this is the third call to 4, the disadvantage and function of closure
The disadvantages of closures are as follows:
The scope is not so intuitive.
There is a memory footprint problem because variables are not garbage collected.
The purpose of the closure is as follows:
You can use a sibling scope
Read the internal variables of other elements
Extend the scope
The above is all the content of the article "how to use closures in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.