python中装饰器的简单介绍

零下一度
零下一度 原创
2017-06-25 10:17:54 843浏览

装饰器前奏1

定义:本质是函数,用来装饰其它函数,就是为其他函数来添加附加功能

原则:1、不能修改被修饰函数的源代码以及调用方式

装饰器前奏2


import timedef timer(func):def warpper(*args,**kwargs):
            start_time = time.time()
            func()
            stop_time = time.time()print("the func run time is %s" % (stop_time-start_time))return warpper
@timer  #timer(test1)def test1():
    time.sleep(3)print("in the test1")
test1()

实现装饰器只是储备:

1、函数即”变量“

2、高阶函数

3、嵌套函数

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

 

装饰器前奏3

装饰器前奏4

以上就是python中装饰器的简单介绍的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。