This article mainly introduces the Python basic tutorial closure of the use of relevant information, I hope that you can help everyone through this article, the need for friends can refer to the next
How to use the closure of the basic Python tutorial
Objective:
Closure (closure) is an important grammatical structure of functional programming. Functional programming is a programming paradigm (both procedural programming and object-oriented programming are also programming paradigms). In process-oriented programming, we have seen functions (function), and in object-oriented programming we have seen objects (object). The fundamental purpose of functions and objects is to organize the code in some logical way and to improve the code's repeatability (reusability). Closures are also a structure for organizing code, which also improves the reusable nature of code.
Different languages implement closures in different ways. Python is based on a function object and provides support for the syntactic structure of closures (we have seen Python use objects for some special syntax in special methods and multiple paradigms). Python everything is object, function This syntax structure is also an object. In a function object, we use a function object like a normal object, such as changing the name of a function object, or passing a function object as a parameter.
Scope of Function Object
Like other objects, a function object has its own scope, that is, the scope of the function object. A function object is defined with a DEF statement, and the scope of the function object is the same as the level at which the def resides. For example, the following code, which is defined within the membership scope of the LINE_CONF function, can only be called within the LINE_CONF's membership range.
Def line_conf (): def line (x): return 2*x+1 print (line (5)) # within the scopeline_conf () print (line (5) ) # Out of the scope
The line function defines a linear (y = 2x + 1). As you can see, the line function can be called in line_conf (), and calling line outside the scope will have the following error:
Nameerror:name ' line ' isn't defined
The description is now outside the scope.
Similarly, if you use lambda to define a function, the scope of the function object is the same as the level at which the Lambda resides.
Closed Package
A function is an object, so it can be returned as a result of a function.
Def line_conf (): def line (x): return 2*x+1 return line # return a function Objectmy_line = line_conf () Print (My_line (5))
The above code can run successfully. The return result of the line_conf is assigned to the line object. The above code will print 11.
What happens if the external variables are referenced in the definition of line ()?
Def line_conf (): b = def line (x): return 2*x+b return line # return a function OBJECTB = 5my_line = Line_conf () Print (My_line (5))
As we can see, the line definition of the subordinate program block refers to the high-level variable B, but B information exists outside the definition of line (b is not defined in the line's membership block). We call b the environment variable for line. In fact, when line is the return value of line_conf, the value of B is included in line (although B is not affiliated with line).
The above code will print 25, which means that the B value referenced by line is the B value that is available for reference when the function object is defined , rather than the B value when used.
When a function is combined with its environment variables, it forms a closure (closure). in Python, the so-called closure is a function object that contains the value of an environment variable. The environment variable value is stored in the __closure__ property of the function object. For example, the following code:
Def line_conf (): b = def line (x): return 2*x+b return line # return a function OBJECTB = 5my_line = Line_conf () print (my_line.__closure__) print (my_line.__closure__[0].cell_contents)
The __closure__ contains a tuple (tuple). Each element in this tuple is an object of type cell. We see that the first cell contains the integer 15, which is the value of the environment variable B when we created the closure.
Let's look at a practical example of a closure:
Def line_conf (A, B): def line (x): return ax + b return lineline1 = line_conf (1, 1) line2 = line_conf (4, 5) print (Line1 (5), line2 (5))
In this example, the function line and the environment variable A, a and B constitute the closure. When we created the closure, we explained the values of the two environment variables through the line_conf parameter, a and B, so that we determined the final form of the function (y = x + 1 and y = 4x + 5). We only need to change the parameter, a, B, we can get a different line expression function. Thus, we can see that closures also have the effect of improving code reusability.
Without closures, we need to explain a,b,x each time we create a line function. In this way, we need more parameter passing and less portability of the code. With closures, we actually created the functional. The line function defines a broad-sense function. Some aspects of this function have been determined (must be straight lines), but others (such as the A and B parameters are pending). We then determine the final function in the form of a closure, based on the parameters passed by line_conf.
Closures and parallel operations
Closures effectively reduce the number of parameters required for the function to be defined. This is of great significance for parallel computing. In the context of parallel computing, we can make each computer responsible for a function and then concatenate the output of a computer with the input of the next computer. In the end, we work like an assembly line, inputting data from one end of a serial computer cluster and outputting data from the other. This scenario is best for a function that has only one parameter input. This can be achieved by closing the package.
Parallel operations are called a hotspot. This is also an important reason why functional programming is hot. Functional programming has existed since the 1950 's, but it is not widely used. However, the pipelined work-parallel clustering process we described above is suitable for functional programming. Due to the natural advantage of functional programming, more and more languages are beginning to join the support of functional programming paradigm.