The example in this article describes the closure usage in Python. Share to everyone for your reference. The specific analysis is as follows:
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.
Python instance:
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_ Adder () print p (+) print Q (100)
Operation Result:
123144
Analysis:
We found that Make_adder is a function, including a parameter addend, the more special place is the function inside the definition of a new function, the new function inside a variable is exactly the external make_adder parameters. That is to say, The externally passed Addend parameter has been bound together with the Adder function, forming a new function, we can think of addend as a new function of the configuration information, configuration information, function is different, that is, can get custom function.
Looking at the results of the operation, we find that although P and Q are all make_adder generated, 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 the Hello function, the value of count[0] is added 1. Maybe you have a question: why not just write count and use a list? This is a bug in Python2, and if you do not use a list, you will be quoted an error:
Unboundlocalerror:local variable ' count ' referenced before assignment.
What do you mean? That is, the conut variable you do not define the direct reference, I do not know what this is a thing, the program collapsed. So, again python3 inside, introduced a keyword: nonlocal, what is this key word? Just tell the Python program, My count variable is externally defined, you go outside to find it. Then Python went to the outer function to find, and then found the count=0 this definition and assignment, the program can be executed normally.
Python3 Code
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 "" "+ fn () +"" return wrappeddef makeitalic (FN): def Wrapped (): return "" + fn () + "" " return wrapped@makebold@makeitalicdef Hello () : Return" Hello world "Print Hello" ()
Execution results
Hello World
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, lists pushed to, closures, anonymous functions, etc., in the development of using, you may achieve a multiplier effect!
Hopefully this article will help you with Python programming.