Closure of Python deep learning and deep learning of python

Source: Internet
Author: User

Closure of Python deep learning and deep learning of python

Closure is an important syntax structure for functional programming. Functional programming is a programming paradigm (both process-oriented and object-oriented programming are programming paradigms ). In process-oriented programming, we have seen functions; in object-oriented programming, we have seen objects ). The fundamental purpose of functions and objects is to organize code in a logical way and improve code reusability ). Closure is also a structure for organizing code, which also improves code reusability.

Different Languages implement closures in different ways. Python is based on function objects and supports the closure syntax structure (in special methods and multi-paradigm, it has been seen many times that Python uses objects to implement some special syntaxes ). Python is an object. The syntax structure of a function is also an object. In a function object, we use a function object like a common object, such as changing the name of a function object or passing a function object as a parameter.

Function object scope

Like other objects, function objects also have their survival range, that is, the scope of function objects. Function objects are defined using def statements. The scope of function objects is the same as that of def. For example, in the following code, the line function defined in the scope of the line_conf function can only be called within the scope of the line_conf function.

Copy codeThe Code is as follows:
Def line_conf ():
Def line (x ):
Return 2 * x + 1
Print (line (5) # within the scope


Line_conf ()
Print (line (5) # out of the scope

The line function defines a straight line (y = 2x + 1 ). We can see that the line function can be called in line_conf (), and the following error will occur when line is called out of scope:
Copy codeThe Code is as follows:
NameError: name 'line' is not defined

It indicates that it is out of scope.

Similarly, if lambda is used to define a function, the function object scope is the same as that of lambda.

Closure

A function is an object, so it can be returned as a function.

Copy codeThe Code is as follows:
Def line_conf ():
Def line (x ):
Return 2 * x + 1
Return line # return a function object

My_line = line_conf ()
Print (my_line (5 ))

The above code runs successfully. The result returned by line_conf is assigned to the line object. The above code will print 11.

What happens if the definition of line () References external variables?

Copy codeThe Code is as follows:
Def line_conf ():
B = 15
Def line (x ):
Return 2 * x + B
Return line # return a function object

B = 5
My_line = line_conf ()
Print (my_line (5 ))

We can see that the line-defined membership block references the High-Level variable B, but the B information exists outside the line definition (the B definition is not in the line-defined membership block ). We call B an environmental variable of line. In fact, when line is the return value of line_conf, line already contains the value of B (although B is not affiliated with line ).

The above code will print 25, that is, the B value referenced by line is the B value that can be referenced when the function object is defined, rather than the B value used.

A function is combined with its environment variables to form a closure ). In Python, a closure is a function object that contains the values of environment variables. The environment variable value is stored in the _ closure _ attribute of the function object. For example, the following code:

Copy codeThe Code is as follows:
Def line_conf ():
B = 15
Def line (x ):
Return 2 * x + B
Return line # return a function object

B = 5
My_line = line_conf ()
Print (my_line. _ closure __)
Print (my_line. _ closure _ [0]. cell_contents)

_ Closure _ contains a tuple ). Each element in this tuples is a cell-type object. We can see that the first cell contains an integer of 15, that is, the value of environment variable B when we create a closure.

The following is an example of a closure:
Copy codeThe Code is as follows:
Def line_conf (a, B ):
Def line (x ):
Return ax + B
Return line

Line1 = line_conf (1, 1)
Line2 = line_conf (4, 5)
Print (line1 (5), line2 (5 ))

In this example, the line function and the environment variables a and B constitute the closure. When creating a closure, we use the line_conf parameters a and B to describe the values of these two environment variables. In this way, we have determined the final form of the function (y = x + 1 and y = 4x + 5 ). We only need to transform the parameters a and B to obtain different linear expression functions. As a result, we can see that closures also improve code reusability.

If no closure exists, we need to describe a, B, and x each time we create a linear function. In this way, we need more parameter passing and reduce code portability. With the closure, we actually created a functional. The line function defines a function of broad significance. Some aspects of this function have been determined (must be a straight line), but other aspects (such as parameters a and B to be determined ). Then, we determine the final function in the form of a closure based on the parameters passed by line_conf.

Closure and parallel operations

The closure effectively reduces the number of parameters required for the function. This is of great significance for parallel operations. In a parallel computing environment, we can let each computer take charge of a function, and then connect the output of a computer with the input of the next computer. In the end, we work like an assembly line to input data from one end of the connected computer cluster and output data from the other end. Such a situation is most suitable for functions with only one parameter input. Closure can achieve this purpose.

Parallel operations are called hot spots. This is also an important reason for the popularity of functional programming. Functional Programming already exists in the 1950 s, but it is not widely used. However, the assembly-line working parallel cluster process described above is suitable for functional programming. Due to the natural advantage of functional programming, more and more languages have begun to support the functional programming paradigm.


For python books

More than half a year of python programming practices. Now it is time to have a better understanding of python.
1. Read the entry book "a byte of python".
I first read python and read it again. I mainly look at the directory to reflect on the degree of understanding at that time and the misunderstanding of learning.
2. go deep into dive into python
I have read some of them, but I have not gone deep into python and have not understood the Special Programming features of python.
Deep Learning of python features, especially opening packages/closures, generators, iterators, and serialization.
3. Interview with the python developer of the design concept "master mind of programming"
Understand python design concepts
Character encoding
Chinese character encoding is always a headache and requires special learning.
4. Programming Practice, calibre series index page
Obtain tags/sequences from calibre metadata, generate separate pages, and provide hyperlinks.
Python source code analysis
A real deep understanding of python
5. python Software Engineering expert programming

How to pass an inner function to the outer layer in the python closure function?

Closures are not required to implement this function.

Def fun (callback, * x ):
Return callback (* x)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.