Python(十) 函数式编程: 匿名函数、高阶函数、装饰器

一、lambda表达式
lambda parameter_list: expression
# 匿名函数

def add(x,y):
    return x+y

print(add(1,2))

f = lambda x,y: x+y

print(f(1,2))
二、三元表达式
# x >y ? x :y

# 条件为真时返回的结果 if 条件判断 else 条件为假时的返回结果

x= 2
y=1
r = x if x > y else y
print(r) # 2
三、map
def square(x):
    return x*x

# for x in list_x:
#     square(x)

# map 等价于上面的for 循环并且执行每一次函数  对于 所传 集合和序列的每一项 都执行 square 这个函数 并且 返回这个函数的每一个返回值
r = map(square, list_x)
print(r) # <map object at 0x0000014E44D86630>
print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]
四、map与lambda
list_x= [1,2,3,4,5,6,7,8]

def square(x):
    return x*x

# 匿名函数 替代 square 函数
r = map(lambda x: x*x, list_x)
print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]
list_x= [1,2,3,4,5,6,7,8]
list_y= [1,2,3,4,5,6,7]

def square(x):
    return x*x

# 匿名函数 替代 square 函数 map(func, *iterables)
r = map(lambda x,y: x*x +y, list_x,list_y)
print(list(r)) # [2, 6, 12, 20, 30, 42, 56, 72]
五、reduce
from functools import reduce

list_x= [1,2,3,4]

# reduce 连续计算,连续调用lambda
r= reduce(lambda x,y: x+y,list_x)
# reduce计算过程 连续计算
# 1+2=3
# 3+3=6
# 6+4=10
print(r) # 10

r= reduce(lambda x,y: x*y,list_x)
print(r) # 24

#第三个参数 是初始值
r= reduce(lambda x,y: x*y,list_x,10)
print(r) # 240
 
六、filter
#filter 过滤

list_x= [1,0,1,0,0,1]
# 根据函数返回真假  判断当前元素是否包含在集合里面
r = filter(lambda x: True if x==1 else False, list_x)
print(list(r)) # [1, 1, 1]
七、命令式编程vs函数式编程
map reduce filter
lambda 算子
 
 
八、装饰器 一
引入装饰器概念
#装饰器  很频繁 很实用
# 类似C#特性 
# 对修改是封闭的, 对扩展是开放的
import time

def f1():
    print("hello")

def f2():
    print("hello")

def print_time(func):
    print(time.time())
    func()

print_time(f1)
print_time(f2)

结果:
1534744900.0469174
hello
1534744900.0469174
hello
九、 装饰器 二
import time

def decortator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

def f1():
    print("hello")

f =  decortator(f1)
f()

结果:
1534745267.9373472
hello
十、装饰器 三
import time

def decortator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

# 装饰器 特别的 语法
@decortator
def f1():
    print("hello")

f1()

结果:
1534745521.8246493
hello

AOP 设计模式

十一、装饰器 四

 多参数输入

import time

def decortator(func):
    def wrapper(*args):
        print(time.time())
        func(*args)
    return wrapper

# 装饰器 特别的 语法
@decortator
def f1(func_name):
    print("hello"+func_name)

@decortator
def f2(func_name,func_name2):
    print("hello"+func_name+func_name2)

f1('f1name')
f2('name','name2')

结果:
1534746043.7318072
hellof1name
1534746043.7318072
hellonamename2
十二、装饰器 五
import time
#*args 多参数, **kw 关键字
def decortator(func):
    def wrapper(*args, **kw):
        print(time.time())
        func(*args, **kw)
    return wrapper

# 装饰器 特别的 语法
@decortator
def f1(func_name):
    print("hello"+func_name)

@decortator
def f2(func_name,func_name2):
    print("hello"+func_name+func_name2)

@decortator
def f3(func_name,func_name2, **kw):
    print("hello"+func_name+func_name2)
    print(kw)


f1('f1name')
f2('name','name2')
f3('name1','name2',a=1,b=2,c='123')

结果:
1534746321.3270056
hellof1name
1534746321.3270056
hellonamename2
1534746321.3279607
helloname1name2
{'a': 1, 'b': 2, 'c': '123'}
十三、装饰器 六

 api接口

身份验证,防止攻击等

装饰器 很重要

原文地址:https://www.cnblogs.com/zhangtaotqy/p/9502983.html