Python装饰器讲解

定义:本质是函数,就是为其他函数添加附加功能。
原则:1.不能修改被装饰的函数的源代码

    2.不能修改被装饰的函数的调用方式

import time
def timmer(func):
	def wrapper(*args,**kwargs) 
		start_time=time.time()
		func()
		stop_time=time.time()
		print('the func run time is %s' %(stop_time-start_time) )
	return warpper

@timmer	  ##调用方式
test1():
	time.sleep(3)
	print('in the test1')	
test1()

实现装饰器需要具备知识点:

实现装饰器只是储备:
1.函数即“变量”
2.高阶函数
3.嵌套函数

高阶函数+嵌套函数=>装饰器

  

 

    

相关文章:

  • 2022-02-28
  • 2021-07-18
  • 2021-12-07
  • 2021-07-17
猜你喜欢
  • 2021-10-31
  • 2021-06-13
  • 2023-02-09
  • 2023-02-16
  • 2021-05-31
  • 2021-05-20
  • 2022-01-18
相关资源
相似解决方案