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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to master Python closures". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to master Python closures.
What is a closure? What's the use of closures? Why use closures? Today, we will take these three questions to understand the closure step by step.
Closures are closely related to functions. Before introducing closures, it is necessary to introduce some background knowledge, such as nested functions, scope of variables and so on.
Scope
Scope is the scope that variables can be accessed when the program is running, the variables defined in the function are local variables, the scope of local variables can only be within the scope of the function, it can not be referenced outside the function.
The variables defined in the outermost layer of the module are global variables, which are visible in the global scope, and of course, global variables can also be read in the function. For example:
Num = 10 # global scope variable
Def foo ():
Print (num) # 10
Local variables cannot be accessed outside the function. For example:
Def foo ():
Num = 10
Print (num) # NameError: name 'num' is not defined nested function
Functions can be defined not only in the outermost layer of the module, but also in the interior of another function. Functions like this are called nested functions (nested function), for example:
Def print_msg ():
# print_msg is a peripheral function
Msg = "zen of python"
Def printer ():
# printer is a nested function
Print (msg)
Printer ()
# output zen of python
Print_msg ()
For a nested function, it can access non-local variables declared in its outer scope, such as the variable msg in the code example that can be accessed normally by the nested function printer.
Is it possible that local variables can be accessed even if they are outside the scope of the function itself? The answer is closure.
What is closure?
As the first type of object, a function can be returned as the return value of the function. Now let's consider the following example:
Def print_msg ():
# print_msg is a peripheral function
Msg = "zen of python"
Def printer ():
# printer is a nested function
Print (msg)
Return printer
Another = print_msg ()
# output zen of python
Another ()
This code has exactly the same effect as the previous example, with the same output of "zen of python". The difference is that the inner function printer is returned directly as a return value.
In general, local variables in a function are available only during the execution of the function, and once print_msg () is executed, we assume that the msg variable will no longer be available. However, here we find that after the execution of print_msg, the value of the msg variable is output normally when another is called, which is the function of closure, which makes it possible for local variables to be accessed outside the function.
After looking at this example, let's define the closure, which is explained on Wikipedia:
In computer science, Closure is the abbreviation of lexical closure (Lexical Closure), which is a function that refers to free variables. The referenced free variable will exist with the function, even if it has left the environment in which it was created. Therefore, there is another saying that a closure is an entity composed of a function and its associated reference environment.
The another here is a closure, which is essentially a function, which consists of two parts, the printer function and the variable msg. Closures keep the values of these variables in memory all the time.
A closure, as its name implies, is a closed package that contains free variables, just like the property values defined in the class. The visible range of free variables is accompanied by the package, where the package can be accessed.
Why use closures?
Closures avoid the use of global variables; in addition, closures allow functions to be associated with some of the data (environments) on which they operate. This is very similar to object-oriented programming, where objects allow us to associate certain data (object properties) with one or more methods.
In general, using closures is a better choice when there is only one method in the object. Let's look at an example:
Def adder (x):
Def wrapper (y):
Return x + y
Return wrapper
Adder5 = adder (5)
# output 15
Adder5 (10)
# output 11
Adder5 (6)
This is more elegant than using classes, and the decorator is also an application scenario based on closures.
All functions have a _ _ closure__ attribute, and if this function is a closure, it returns a tuple object made up of cell objects. The cell_contents property of the cell object is a free variable in the closure.
> adder.__closure__
> adder5.__closure__
()
> adder5.__closure__ [0] .cell _ contents
five
This explains why the local variable can be accessed outside the function after it is separated from the function, because it is stored in the cell_contents of the closure.
Thank you for reading, the above is the content of "how to master Python closures". After the study of this article, I believe you have a deeper understanding of how to master Python closures, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.