Python closure detailed

Source: Internet
Author: User
Tags closure

Closure (closure) is an important grammatical structure of functional programming. Functional programming is a programming paradigm (while process programming and object-oriented programming are all programming paradigms). In process-oriented programming, we have seen functions (function); In object-oriented programming, we have seen objects (object). The fundamental purpose of functions and objects is to organize code in some logical way and to improve the reusable nature of code (reusability). Closures are also a structure for organizing code, which also increases the reusable nature of code.

Different languages implement closures in different ways. Python is based on a function object and supports the syntactic structure of closures (we've seen Python use objects to implement some special syntax in special methods and multiple paradigms many times). Python is all about objects, and the grammatical structure of functions 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 objects

Like other objects, a function object has a scope for its existence, that is, the function object. function objects are defined using the DEF statement, and the function object is scoped to the same level as the def. For example, the following code, which we define in the line_conf function, can only be invoked within the LINE_CONF's membership scope.

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 Lines function defines a line (y = 2x + 1). As you can see, the line function can be called in line_conf (), while calling line outside the scope will have the following error:

Nameerror:name ' line ' isn't defined

Description is already outside the scope.

Similarly, if you define a function by using a lambda, the function object's scope is the same as the level at which the lambda is located.

Closed Bag

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 object
     
my_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 code above will print 11.

What happens if you refer to an external variable in the definition of line ()?

Def line_conf ():
    b =
    def line (x): Return
        2*x+b return line # return       a function object
     
B = 5
my_line = line_conf ()
Print (My_line (5))

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.