Rookie chews the basics of Python to continue.
First, return function
The third chapter has simply manipulated Python's higher-order functions, and continues to practiced hand functions as the return value of the effect; Generally, higher-order functions can be returned as result values, in addition to receiving functions as parameters.
Case: Usually we write a sum function that writes
#通常我们写一个求和的函数会这样写def get_sum (*param): count = 0; For item in param: count = Count + Item return Countprint (Get_sum (1,2,3,4))
Output: 10
#但是, if not to sum, but in the back of the code as needed in the calculation can be, can. Instead of returning a summation result, it returns the SUM function. def last_sum (*param): def sum (): num = 0 for n in param: num = num + n return num return sumfun = Last_sum (1,2,3,4) print (fun) print (fun ())
Output Result:
<function Last_sum.<locals>.sum at 0x7f4a2d3b58c8>10
Description: This shows that the function returned by the simple output is no result, only the function returned can be called to print the sum
In addition, if you call the Last_sum function multiple times, each return is a new function, even if the same parameters are passed in;
For example:
FUN1 = Last_sum (1,2,3,4) fun2 = Last_sum (1,2,3,4) print (fun1 = = fun2)
return: False
Description: Calls to Fun1 () and fun2 () do not affect each other
Closed Package
def count (): fs = [] for I in Range (1,4): def f (): return i * I fs.append (f) return FSF1, F2 , F3 = count () print (F1 ()) print (F2 ()) Print (F3 ())
Output: 9,9,9
Note: This is because when a function returns a function, I this internal local variable is also referenced by a new function, and when three functions are returned, function I is already 3, so the function output 9, 9, 9
Note: The return function does not reference any of the loop variables, or any subsequent variables that change
It can be written like this:
Def count2 (): def f (i): def g (): return i * I return g fs = [] for I in range (1,4): fs.append ( f (i)) return fsf11, f22, F33 = Count2 () print (F11 ()) print (F22 ()) print (F33 ())
return Result: 1,4,9
Description: 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, the value that has been bound to the function parameter is unchanged
That is, when we have to call the loop variable, we need to add a local function, each time I execute the loop variable i to call the new function and I as one of its parameters to be executed immediately, then I will not be affected regardless of the late.
Second, Anonymous Functions
Anonymous functions are functions that do not have a name, and sometimes you do not define functions for brevity and convenience, but instead use anonymous functions directly, for example:
#匿名函数mylist = List (map (lambda y:y*y, [1,2,3,4])) print (mylist)
Output: [1, 4, 9, 16]
Description: Anonymous function lambda y:y*y is actually an anonymous function, using lambda to represent an anonymous function, the first y represents the parameters of an anonymous function, and the colon is followed by an anonymous body, which can represent functions handled by the function.
An anonymous function is an expression that does not have to be written Returen the return value is the expression result, and the anonymous function can also be a variable, for example:
#匿名函数-Variable mytest = lambda y:y*ymylist = List (map (mytest, [1,2,3,4])) print (mylist)
Output: [1, 4, 9, 16] Ibid.
three, the decoration device
The function is actually an object. And a function object can be assigned to a variable, so a variable is the equivalent of calling that function.
For example:
Def myfun (a): return a*atest = Myfunprint (Test (5))
Output: 25
In fact, the function has a __name__ property that can verify who his real function is. For example:
Print (test.__name__)
Output: Myfun
Python Basic Learning (iv)