Python core programming-closures

Source: Internet
Author: User

Baidu searched the concept of closures: in short, the function of closures is that after the external function is executed and returned, the closing mechanism does not retract the resource occupied by the function, because the execution of the intrinsic function relies on the variables in the outer function. This is a very straightforward description of the effect of closures, unprofessional and not rigorous, but the general meaning is that, understanding closures need a gradual process.
My own understanding, is to show themselves, but also hope that the great God pointing out: closure of the external function to return the content of the number, the inner function used the parameters of the outer function, and does not declare a parameter outside the entire function to save the value of the execution of this method, only when the declaration outside the function to get the handle of the value of the The outer and inner functions are referenced by each other without disturbing the outside world. Like the first code below, when the count1 is not recycled, 5 of this value is always referenced by the count1, in count1 not be recycled by garbage collection mechanism until the effective, the outside world can not change this value, if in life a count2, that is count2 own field, It's totally okay with count1.

In the reading of the closure of the problem, found to give such a code:

def counter (start_at=0):    = [Start_at]    def  incr ():          Print  count        + = 1        return  count[0    ]return= Counter (5) Count1 ()
Results: 6

The result is the same as expected. But a problem has been found, Count = [Start_at] This place is encapsulated with an array, why do you want to do so, so I modified it, the array removed

 def  counter (start_at=0): Count  =< Span style= "COLOR: #000000" > start_at  def   INCR ():  print   count count  + = 1 return   count  return   Incrcount1  = counter (5 print  count1 () 

(For a study of the following questions, refer to http://linluxiang.iteye.com/blog/789946 )
Run Error: unboundlocalerror:local variable ' count ' referenced before assignment
Cause the compiler sees a=a+1 and checks to the right to find a variable of a. The compiler is based on its own rules (if a name appears in a parameter declaration, an assignment statement (left), a function declaration, a class declaration, an import statement, a for statement, and a except statement, this is a local variable. )。 The compiler looked through it, found a on the right of A=a+1, and decided that he was a local variable. The compiler successfully assigns a value, but when the virtual machine is running, the virtual machine does not find a statement when computing a=a+1, and then it gives an error. Why arrays are normal, because only a reference is in the array. Python name lookup order: first local, then closed, at global.

Python core programming-closures

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.