Day11--进阶--匿名函数、高阶函数和装饰器
🧸匿名函数:
主要是使用lambda表达式
def add(x, y):
return x + y
s = add(1, 2)
print(s)
# 匿名函数
f = lambda x, y: x + y
s1 = f(1, 2)
print(s1)
🎈三元表达式:
# 三元表达式
# 别的编程语言中 x > y ? x: y
x = 2
y = 1
r = x if x > y else y
print(r) # 2
🧸高阶函数
🎈map
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
def my_square(x):
return x * x
# map
r = map(my_square, list_x)
print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]
🎈map 和 lambda
# 示例1
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
r = map(lambda x: x*x, list_x)
print(list(r)) # [1, 4, 9, 16, 25, 36, 49, 64]
# 示例2
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
list_y = [1, 2, 3, 4, 5, 6, 7, 8]
r = map(lambda x, y: x*x+y, list_x,list_y)
print(list(r)) # [2, 6, 12, 20, 30, 42, 56, 72]
# 示例3
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
list_y = [1, 2, 3, 4, 5, 6]
r = map(lambda x, y: x*x+y, list_x,list_y)
print(list(r)) # [2, 6, 12, 20, 30, 42]
# 示例4
list_x = [1, 2, 3, 4, 5, 6]
list_y = [1, 2, 3, 4, 5, 6, 7, 8]
r = map(lambda x, y: x*x+y, list_x,list_y)
print(list(r)) # [2, 6, 12, 20, 30, 42]
# 计算结果取列表较少的那个
🎈reduce:
# reduce
from functools import reduce
# reduce 是一个连续计算,连续调用lambda
list_x = [1, 2, 3, 4, 5, 6, 7, 8]
r = reduce(lambda x, y: x + y, list_x)
# 1+2=3 --> 3+4=7 --> 7+5=12 -->此时x=12,y=6,.....依次类推
print(r) # 36
# 如
list_x1 = ['1', '2', '3', '4', '5', '6']
r1 = reduce(lambda x, y: x + y, list_x1, 'aaa') # aaa123456
print(r1)
# 旅行者问题。坐标(x, y), 此时可以使用reduce来计算
[(1, 3), (2, -2), (-2, 3)]
🎈filter:
list_x = [1, 1, 0, 1, 1, 0, 1, 0]
# 剔除list_x中0元素
# r = filter(lambda x:True if x == 1 else False, list_x) # [1, 1, 1, 1, 1]
r = filter(lambda x: x, list_x) # [1, 1, 1, 1, 1]
print(list(r))
list_s = ['a', 'A', 'b', 'B', 'c', 'C']
# 剔除list_s中小写字母
r1 = filter(lambda x: True if x >= 'A' and x <= 'Z' else False, list_s)
print(list(r1))
🎈小结:函数式编程 和 命令式编程
函数式编程 如 map reduce filter (前三个函数) lambda(算子)
命令式编程 如 def if else for
🧸装饰器:
🎈使用例子引入:
# 在f1()中打印当前时间
import time
def f1():
print('This is a function')
print(time.time()) # unix 时间戳
# f1()
def f2():
print('This is a function222')
def f3():
print('This is a function333')
# 例:要在很多个函数里打印当前的执行时间,应如何做?(每个函数加time不可取)
# 要遵循 对修改是封闭的,对扩展是开放的
# 方法1 引入新的函数
def print_curent_time(func):
print(time.time())
func()
print_curent_time(f2)
print_curent_time(f3)
# --------输出--------
# 1652840584.4972363
# This is a function222
# 1652840584.4972363
# This is a function333
# 此方法 和下面的打印区别不大
print(time.time())
f2()
print(time.time())
f3()
# --------输出--------
# 1652840584.4972363
# This is a function222
# 1652840584.4972363
# This is a function333
🎈一个简单的装饰器:
# 装饰器
import time
def decorator(func):
def wrapper():
print(time.time())
func()
return wrapper
# # 原生调用
# def f1():
# print('This is a function111')
#
# w = decorator(f1)
# w()
# 使用语法糖 装饰器
@decorator # 像挂件一样在f1()上,叫做装饰器
def f1():
print('This is a function111')
f1()
# ------输出-----
# 1652841593.1164718
# This is a function111
# 了解AOP
🎈含参数的装饰器:
补充:如
**kwargs | 关键字参数 |
---|---|
*args | 可变参数 |
# 带参数的装饰器
import time
def decorator(func):
def wrapper(*args, **kwargs):
print(time.time())
func(*args, **kwargs)
return wrapper
# 无参数
@decorator # 像挂件一样在f1()上,叫做装饰器
def f0():
print('This is a function00')
# 含有一个参数
@decorator # 像挂件一样在f1()上,叫做装饰器
def f1(func_name):
print('This is a function'+ func_name)
# 含有两个参数
@decorator
def f2(func_name1, func_name2):
print('This is a function'+ func_name1+func_name2)
# 含有两个参数 和 关键字参数
@decorator
def f3(func_name1, func_name2, **kwargs):
print('This is a function'+ func_name1+func_name2)
print(kwargs)
f0()
# --------输出---------
# 1652843595.5193594
# This is a function00
f1('111')
# --------输出---------
# 1652843595.5193594
# This is a function111
f2('22', '222')
# --------输出---------
# 1652843595.5193594
# This is a function22222
f3('33', '333', a=31, b=32, c = '3113')
# --------输出---------
# 1652843595.5193594
# This is a function33333
# {'a': 31, 'b': 32, 'c': '3113'}
装饰器很好的体现了AOP思想