Python开发-day20-函数
生成器
生成器总结
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 8:58 3 # @Author:Henry Scofield 4 # @File:2.py 5 # @Software:PyCharm 6 7 age = 10 8 res = True if age > 10 else False 9 List = ['a' for i in range(10)] 10 g_List = ('a' for i in range(10)) # 生成器表达式 11 12 13 def test(): 14 for i in range(4): 15 yield i 16 17 18 t = test() # t生成器对象 19 20 for i in t: # 如果这里遍历完了,下面list(t1)就是空列表了! 21 print(i) 22 23 # print(t) #24 t1 = (i for i in t) # t1 生成器对象 25 # print(t1) # at 0x0000019A4A229A50> 26 print(list(t1)) # [0, 1, 2, 3]
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 8:58 3 # @Author:Henry Scofield 4 # @File:2.py 5 # @Software:PyCharm 6 7 age = 10 8 res = True if age > 10 else False 9 List = ['a' for i in range(10)] 10 g_List = ('a' for i in range(10)) # 生成器表达式 11 12 13 def test(): 14 for i in range(4): 15 yield i 16 17 18 t = test() # t生成器对象,要迭代才有值,迭代完就没值了 19 20 t1 = (i for i in t) 21 t2 = (i for i in t) 22 print(list(t1)) # [0, 1, 2, 3] # 如果这里遍历完了,下面list(t1)就是空列表了! 23 print(list(t2)) # []
迭代器协议
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 8:39 3 # @Author:Henry Scofield 4 # @File:def函数.py 5 # @Software:PyCharm 6 def test(): 7 yield 1 # 相当于return 1 8 yield 2 9 # print("") 10 11 12 t = test() 13 14 15 def run(): 16 print("from run") 17 print("from run") 18 print("from run") 19 20 21 run() 22 23 24 def test(): 25 print("from run") 26 yield 1 # 相当于return 相当于保存状态,next()一次用一次 27 print("from run") 28 yield 2 29 # print("") 30 31 32 t = test() 33 34 # 迭代器协议 可迭代对象 35 next(t) 36 37 print("abcd") 38 print("abcd") 39 print("abcd") 40 print("abcd") 41 42 next(t) 43 44 # t.__next__() 从上次迭代后的位置开始迭代。 # StopIteration 已经走完了两个print,在迭代会报错 45 # t.send('123')# StopIteration 已经走完了两个print,在迭代会报错
装饰器
迭代器:
装饰器:本质就是函数,功能:为其他函数添加附加功能。
原则:
1.不修改被修饰函数的源代码;
2.不修改被修饰函数的调用方式。
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 9:21 3 # @Author:Henry Scofield 4 # @File:3.py 5 # @Software:PyCharm 6 import time 7 8 9 # 被修饰函数上线后,不能随意再改源代码 10 def cal(l): 11 start_time = time.time() 12 res = 0 # 初始为0 13 for i in l: 14 time.sleep(0.1) 15 res += i # 求和 16 stop_time = time.time() 17 print("函数的运行时间是 %s " % (stop_time - start_time)) 18 return res 19 20 21 print(cal(range(100))) 22 23 24 def index(): 25 pass 26 27 28 def home(): 29 pass 30 31 32 index() # index()调用就index()调用,不修改被修饰函数的调用方式举例1
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 9:31 3 # @Author:Henry Scofield 4 # @File:4.py 5 # @Software:PyCharm 6 import time 7 8 9 def timmer(func): 10 def wapper(*args, **kwargs): 11 start_time = time.time() 12 res = func(*args, **kwargs) 13 stop_time = time.time() 14 print("函数的运行时间是 %s " % (stop_time - start_time)) 15 return res 16 17 return wapper 18 19 20 @timmer 21 def cal(l): 22 res = 0 # 初始为0 23 for i in l: 24 time.sleep(0.1) 25 res += i # 求和 26 return res 27 28 29 res = cal(range(20)) 30 print(res)举例2
装饰器的知识储备
装饰器 = 高阶函数 + 函数嵌套 + 闭包
高阶函数
1 # -*- coding:utf-8 -*- 2 # @Time:2022/4/1 9:40 3 # @Author:Henry Scofield 4 # @File:5-高阶函数.py 5 # @Software:PyCharm 6 import time 7 8 # def foo(): 9 # time.sleep(3) 10 # print("Hello!") 11 # 12 # 13 # def test(func): 14 # print(func) 15 # start_time = time.time() 16 # func() # 输出参数的值 17 # stop_time = time.time() 18 # print("函数运行时间是 %s " % (stop_time - start_time)) 19 # 20 # 21 # test(foo) 22 # # 输出只有高阶函数满足不了装饰器,函数接收的参数是一个函数名 23 # # Hello! 24 25 # def foo(): 26 # print("from the foo") 27 # 28 # 29 # def test(func): 30 # return func 31 # 32 # 33 # res = test(foo) 34 # # print(res) #35 # # res() # from the foo 36 37 # 多运行了一步 38 import time 39 40 41 def foo(): 42 time.sleep(3) 43 print("来自foo") 44 45 46 # 添加统计运行时间功能 47 # 不修改foo 源代码 48 # 不修改foo 调用方式 49 def timer(func): 50 start_time = time.time() 51 func() # 来自foo 52 stop_time = time.time() 53 print("函数的运行时间是 %s " % (stop_time - start_time)) 54 return func 55 56 57 foo = timer(foo) 58 foo() # 来自foo