Python closure Closure function as return value

Source: Internet
Author: User
Tags closure

function as return value

Higher-order functions can also return a function as a result value, in addition to the ability to accept functions as parameters.

1>>>defLazy_sum (*args):2...defsum ():3... Ax =04... forNinchargs:5.... Ax = ax +N6...returnAx7...returnsum8 ... 9>>> f = lazy_sum (1, 3, 5, 7, 9)Ten>>>F One<function Lazy_sum.<locals>.sum at 0x1014ae730> A>>>f () -25

When we call lazy_sum() , each call will return a new function, even if the same parameters are passed in:

1 >>> f1 = lazy_sum (1, 3, 5, 7)2 >>> F2 = lazy_sum (1, 3, 5, 7)3< /c4> >>> F14 <function lazy_sum.<locals>.sum at 0x1014ae8c8>5 > >> F26 <function lazy_sum.<locals>.sum at 0x1014ae7b8>7 >>> F1 = = F28 False

Second, closed package

In computer science, 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. Closures can have multiple instances at run time, and different reference environments and the same combination of functions can produce different instances.

Simply put, this intrinsic function can use the behavior of an external function variable, called a closure .

In this example, we define the function in the function lazy_sum sum , and the intrinsic function sum can refer to lazy_sum the parameters and local variables of the external function, and when the lazy_sum function is returned sum , the relevant parameters and variables are stored in the returned function, which is called " The program structure of the closure (Closure) has great power.

Notice that the returned function references a local variable within its definition, args so when a function returns a function, its internal local variables are referenced by the new function, so it is not easy to implement them with a simple closure.

One thing to keep in mind when returning closures: The return function does not refer to any loop variables, or to subsequent variables that change.

1>>>defcount ():2... fs = []3... forIinchRange (1, 4):4...deff ():5...returnII6 ... fs.append (f)7...returnFS8 ... 9>>> F1, f2, F3 =count ()Ten>>>F1 () One9 A>>>F2 () -9 ->>>f3 () the9

All of them 9 ! The reason is that the returned function refers to the loop variable i , but it is not executed immediately. When all 3 functions are returned, the variables they refer to are i already turned 3 , so the end result is 9 .

What if you must refer to a loop variable? The method is to create a function that binds the current value of the loop variable with the parameter of the function, regardless of how the loop variable is subsequently changed, and the value that is bound to the function parameter remains the same:

1>>>defcount ():2...defF (j):3...defg ():4...returnJ *J5...returng6... fs = []7... forIinchRange (1, 4):8 ... fs.append (f (i))9...returnFSTen ...  One>>> F1, f2, F3 =count () A>>>F1 () -1 ->>>F2 () the4 ->>>f3 () -9

Python closure Closure function as return value

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.