What is a closure package?
Simply put, closures are based on different configuration information to get different results
Let's take a look at the professional explanation: Closures (Closure) are short for lexical closures (Lexical Closure) and are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created. So, there is another argument that closures are entities that are composed of functions and their associated reference environment.
Look at the concept is always confusing the mind, see a few Python small examples will be
Example 1
def make_adder (addend): def adder (augend): return augend + addend return adderp = Make_adder (all) Q = Make_adde R (+) print p (+) print Q (100)
Run results
123144
Analyze
We find that a make_adder
function, including a parameter addend
, is a special place where a new function is defined, and a variable inside the new function is just an external make_adder
parameter. That is, the externally passed addend
parameter has been adder
Functions are bound together, forming a new function, we can think of as addend
a new function of the configuration information, configuration information, functions of the function is different, that is, the function can be customized.
Looking at the results of the operation, we found that although P and Q are both make_adder
generated, the different results are obtained because the configuration parameters are different and the functions of the same parameters are executed later. This is the closure.
Example 2
def hellocounter (name): Count=[0] def counter (): Count[0]+=1 print ' Hello, ', Name, ', ', str (count[0]) + ' Access! ' return Counterhello = Hellocounter (' ma6174 ') hello () hello () hello ()
Execution results
Hello, YSISL, 1 access! Hello, YSISL, 2 access! Hello, YSISL, 3 access!
Analyze
This program is interesting, we can think of this program as a function to count the number of function calls. count[0]
Can be seen as a counter, without executing hello
the function once, count[0]
the value is added 1. Perhaps you have questions: why not write directly count
and use a list? This is a bug in Python2, and if you don't use a list, it will be reported as a mistake:
UnboundLocalError: local variable ‘count‘ referenced before assignment
.
What do you mean? That conut
is, the variable you do not have a definition of the direct reference, I do not know what this is a thing, the program collapsed. So, again python3 inside, introduced a key word: nonlocal
, this keyword is what? is to tell the Python program, my this count
Variables are defined externally, you go outside and look for them. Then python goes to the outer function and finds the count=0
definition and assignment, and the program executes normally.
python3 代码
def hellocounter (name): count=0 def counter (): nonlocal count count+=1 print ' Hello, ', Name, ', ', str (count[0]) + ' access! ' return Counterhello = Hellocounter (' ma6174 ') hello () hello () hello ()
Example 3
def makebold (FN): Def wrapped (): Return "<b>" + fn () + "</b>" return wrappeddef makeitalic (FN): Def wrapped (): Return "<i>" + fn () + "</i>" return Wrapped@makebold@makeitalicdef Hello (): Retu RN "Hello World" Print Hello ()
Execution results
<b><i>hello world</i></b>
Simple analysis
How is this program familiar? This is not a legendary decorator? Yes, this is the decorator, in fact, the decorator is a kind of closure, we recall the concept of the adorner: the function (parameters, return value, etc.) processing, to generate a function of the enhanced version of a function. Then look at the concept of closures, this enhanced version of the function is not our function after the configuration? The difference is that the adorner parameter is a function or class that specializes in processing classes or functions.
Python has a lot of advanced features, such as decorators, generators, list derivation, closures, anonymous functions, etc., in the development of using, it may achieve a multiplier effect!
The closure function in Python programming