Python entry 16th Day __ anonymous function, return function, closure

Source: Internet
Author: User
Tags closure for in range

anonymous functions

When we pass in a function, there are times when we don't need to explicitly define a function, and it's easier to pass in an anonymous function directly.

In Python, there is a limited amount of support for anonymous functions. As an example of a map() function, when calculating f (x) =x2, in addition to defining a f(x) function, you can also pass in an anonymous function directly:

>>> list (map (lambda x:x * x, [12345678  9]) [14949  [Bayi]

By contrast, the anonymous function lambda x: x * x is actually:

def f (x):     return x * x

The keyword lambda represents an anonymous function, preceded by a colon, that x represents a function parameter.

The anonymous function has a restriction that there can be only one expression, without writing return , and the return value is the result of that expression.

There is a benefit to using anonymous functions because the function does not have a name and does not have to worry about function name collisions. In addition, the anonymous function is also a function object, you can assign the anonymous function to a variable, and then use the variable to invoke the function:

Lambda x:x * x>>> F<function <lambda> at 0x101c6ef28>> >> F (5)25

Similarly, anonymous functions can be returned as return values, such as:

def build (x, y):     return Lambda: x * x + y * y
Practice

Please use the anonymous function to transform the following code:

1 # -*-coding:utf-8-*- 2 3 def is_odd (n): 4     return n% 2 = = 156 L = List (filter (is_odd, range (1)))7  Print(L)

Results:

1 # -*-coding:utf-8-*- 2 3 # def is_odd (n): 4 #      return n% 2 = = 156 L = List (filter (lambda n:n%2==1, Range (1))) /c14>7print(L)
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.

Let's implement the summation of a mutable parameter. In general, the function of summing is defined like this:

def calc_sum (*args):    = 0    for in  args:        = ax + n     return Ax

But what if you don't need to sum it right away, but in the later code, and then calculate it as needed? Instead of returning the result of a sum, you can return a function that sums:

def lazy_sum (*args):    def  sum ():        = 0        for in  args:            = ax + n        return  ax    return sum

When we call lazy_sum() , we return the SUM function instead of summing the result:

>>> f = lazy_sum (1, 3, 5, 7, 9)>>> F<function lazy_sum.<locals>.sum at 0 X101c6ed90>

When the function is called f , the result of the sum is really computed:

>>> F ()25

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.

Note again that when we call lazy_sum() , each call returns a new function, even if the same parameter is passed in:

>>> f1 = lazy_sum (1, 3, 5, 7, 9)>>> F2 = lazy_sum (1, 3, 5, 7, 9)>>> f1==F 2False

f1()and f2() the results of the call are not affected.

Closed Package

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.

Another problem to be aware of is that the returned function is not executed immediately, but only when it is called. f() Let's look at an example:

def count ():     = []    for in range (1, 4):        def  F ():              return i*i        fs.append (f)    return= count ()

In the example above, each loop creates a new function and then returns the 3 functions that were created.

You may think that the call f1() , f2() and the f3() result should be 1 ,, but the 4 9 actual result is:

>>> F1 ()9>>> F2 () 9>>> f3 ()9

All of them 9 ! The reason is that the returned function refers to the 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 .

1 defcount ():2FS = []3      forIinchRange (1, 4):4         deff ():5             returni*I6 fs.append (f)7         8     returnFS9 TenF1 =count () One  A #in list FS, three functions are stored [i*i,i*i,i*i] - #The returned function refers to the variable I, but it is not executed immediately. When all 3 functions return, they refer to the variable i has become 3 - Print(F1[0] ()) the Print(f1[1]()) - Print(F1[2] ())

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.

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         returng6FS = []7      forIinchRange (1, 4):8Fs.append (f (i))#f (i) is executed immediately, so the current value of I is passed in F ()9     returnFs

And look at the results:

>>> F1, f2, F3 = count ()>>> F1 ()1>>> F2 ()4>>> f3 () /c6>9
The disadvantage is that the code is long and can be shortened with a lambda function.
defcount ():defF (j):#def g ():#return j*j        return Lambda: j*J FS= []     forIinchRange (1, 4): Fs.append (f (i))#f (i) is executed immediately, so the current value of I is passed in F ()    returnFSF1=count ()Print(F1[0] ())Print(f1[1]())Print(F1[2] ())

And look at the results:

149
Practice

Use a closure to return a counter function that returns an incrementing integer each time it is called:

Python entry 16th Day __ anonymous function, return function, closure

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.