One, a variable can point to a function
A = ABS
Second, the function can be executed by invoking a variable that points to the function
A = ABS
A (-10)
A variable that points to a function can be passed as a parameter to a function
def add (x,y,f):
return f (x) + f (Y)
Add ( -5,-6,abs)
Four, map () function
The map () function can pass a function and a iterator object to it, and map uses that function to sequence the elements of each iterator.
List1 = [x for x in range (1,11)]
def fun (x):
return x + 1
List2 = Map (fun,list1)
V. Reduce () function
The reduce () function receives a function and a iterator object, but receives a function that has two variables, and sequentially iterates through the two elements of the list each time
Six, filter () function
The filter () function is also a high-order function with two parameters, namely functions and iterator objects, and functions sequentially on each element of the list, depending on whether the function returns True and False to perform the related operation
#使用filter () function to output all the odd numbers in a list:
List1 = [x for x in range (1,101)]
def is_odd (x):
If x% 2! = 0:
Return True
Else
Return False
List2 = List (is_odd,list1)
VII, sorted () function
The sorted () function can be used as a sort
Sorted (can traverse objects)
Sorted (Can traverse object, Key=abs) #按照每个元素的abs进行排序
Sorted (Can traverse object, key=abs, reverse = True) #按照每个元素的abs进行排序并且反转
Viii. function return and closure
Functions can also be returned as values
Closure is an organization of functions as the first-class citizen in functional programming
def welcome (WELCOME_STR):
def Say_hello (user_name):
return WELCOME_STR + user_name
Return Say_hello
Say_hello_fun = Welcome ("Hello")
str = say_hello_fun ("CQ")
Print (str)
IX. specifications for creating closures
Closures must have inline functions
Inline functions need to refer to variables in the first level namespace of the nested function
The closure function must return an inline function
Problems of execution and invocation in the closure function
First step: Get inline functions with the most external functions
Step two: Get the returned execution result by constructing the inline function parameter
Xi. Anonymous functions
Python provides support for anonymous functions through lambda
Lambda parameters: Arguments participating in an expression
A lambda expression can have only one expression and returns the result of the expression directly without writing a return statement
A lambda expression can be assigned directly to a variable, and then a variable is used to construct the function to implement the invocation
Fun = Lambda x:x * x
Fun (5)
12, the decorative device
An adorner is a mechanism for adding functionality to a function based on functions that do not break the original function.
def test1 (fun):
def test2 (*K,**KV):
Print ("Now", fun.__name__, "is run"):
Return Fun (*k)
Return test2
@test1
def test (USER_NAME):
Print ("Hello,", user_name)
Test ("CQ") #这里是调用执行
13. Partial function
By using the partial function, it is convenient to create a new function and make its original function parameters have a fixed default value
Import Functools
Int2 = functools.partial (int,base=2)
Int2 (' 1000 ')
Functools.partial (original function, default value of original function)
Python Learning notes (eight)