Lambda expression
- Start with lambda
- Follow a certain parameter (if any)
- Parameters are separated by a colon and an expression theme
- Just an expression, so, no return
# 计算一个数字的100倍数stm = lambda x: 100 * x stm(89)
Map
- The elements of a collection or list, each of which is manipulated according to a certain rule (function), creating a new list or collection
- The map function is a system-provided function with a mapping function, and the return value is an iterative object
def mulTen(n): return n*10l3 = map(mulTen, l1 ) # map类型是一个可迭代的结构,所以可以使用for遍历for i in l3: print(i) l4 = [i for i in l3]print(l4)# 上面列表生成式得到的结果为空——可迭代结果不能用列表生成式
Reduce
- Merge an iterative object into one final result
- For function requirements as parameters: two parameters + return value
from functools import reduce# 定义一个操作函数,求和def myAdd(x,y): return x + y # 对于列表[1,2,3,4,5,6]执行myAdd的reduce操作rst = reduce( myAdd, [1,2,3,4,5,6] )print(rst)
Filter
- Filtering a set of data, qualifying data (which causes the filter function to return true) generates a new list and returns
- Call Format: Filter (f, data), F is the filter function, data is
def isEven(a): return a % 2 == 0l = [3,4,56,3,2,3,4556,67,4,4,3,23455,43]rst = filter(isEven, l) # 返回的是一个可迭代对象
Sorted
- Sorted (data, key, reverse), Data: Sort object, key: Perform a key function operation on each element before ordering, reverse: If True flips
a = [-43,23,45,6,-23,2,-4345]al = sorted(a, key=abs, reverse=True) # 即按照绝对值的倒叙排列print(al)
return function
def myF4( *args): def myF5(): rst = 0 for n in args: rst += n return rst return myF5f5 = myF4(1,2,3,4,5,6,7,8,9,0)# f5的调用方式f5()
Closures (closure)
def count(): fs = [] for i in range(1, 4): def f(): return i * i fs.append(f) return fsf1, f2, f3 = count()print(f1(), f2(), f3()) # 结果:9 9 9
- The reason for this is that the return function refers to the variable I, I is not immediately executed, but until three functions are returned by the time of uniform use, when I has become 3, the final call, all return is 3*3
- This problem is described as: When a closure is returned, the return function cannot reference any of the loop variables
- Solution: Create a function that binds the current value of the loop variable with the parameter of the function, no matter how the loop variable changes later, the value of the function parameter that has been bound no longer changes
def count(): def f(j): def g(): return j * j return g fs = [] for i in range(1, 4): fs.append(f(i)) return fsf1, f2, f3 = count()print(f1(), f2(), f3()) # 结果:1 4 9
Adorner (Decrator)
# 对hello函数进行功能扩展,每次执行hello万打印当前时间import timedef print_time(f): def wrapper(*args, **kwargs): print("Time: ", time.ctime()) return f(*args, **kwargs) return wrapper@print_timedef hello3(): print('我是手动执行的喵||') returnif __name__ == '__main__': hello3() print('*' * 30) hello3 = print_time(hello3) hello3() print('*' * 30) f = print_time(hello3) f() print('*' * 30) # 结果:'''Time: Sun Aug 26 21:01:18 2018 我是手动执行的喵||******************************Time: Sun Aug 26 21:01:18 2018Time: Sun Aug 26 21:01:18 2018我是手动执行的喵||******************************Time: Sun Aug 26 21:01:18 2018Time: Sun Aug 26 21:01:18 2018Time: Sun Aug 26 21:01:18 2018我是手动执行的喵||******************************'''
Explain the results of the operation:
-
Run Hello3 ():
Time:sun 26 21:01:18 2018
I'm a hand-executed meow | |
Hello3 () use adorners to print the time before running the function interior
-
Run Print_time (HELLO3):
Time:sun 26 21:01:18 2018
Time:sun 26 21:01:18 2018
I'm a hand-executed meow | |
Print_time run print time, return Hello3, again like 1
-
Run Print_time (Print_time (HELLO3)):
Time:sun 26 21:01:18 2018
Time:sun 26 21:01:18 2018
Time:sun 26 21:01:18 2018
I'm a hand-executed meow | |
Add a layer, one more time
Python-Functional programming