装饰器学习

来源:互联网 发布:广州凯申物流 知乎 编辑:程序博客网 时间:2024/05/22 06:37

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

原则:1.不能修改被装饰的函数的源代码

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

实现装饰器知识储备:

1.函数即“变量”,函数在调用前需要先声明

2. 高阶函数

   a:把一个函数名当作实参传递给另外一个函数(在不修改被装饰函数情况下为其增加功能)

def test1():

    print('in the test1')

def test2(func):   # func = test1

    res = func()   # func() = test1()

    return res

test2(test1)

结果:in the test1                                                                                                                                 

   b:返回值中包含函数名(不修改函数的调用方式)

def test1():

    print('in the test1')

def test2(func):   # func = test1

    return func

test1 = test2(test1)                                                                                                                               

test1()

结果:in the test1

3. 嵌套函数:在一个函数内声明一个函数

def test1():

    def test2():

        print("in the test2")

    test2()

test1()

结果:in the test2                                                                                                                                 

内嵌函数的作用在函数内部,不可以在外部进行调用,如下面则是错误的:

def test1():

    def test2():

        print("in the test2")

test1()

test2()    # 该函数作用域在函数test1内部,超出该范围则会报错                                                 

结果:NameError: name 'test2' is not defined

如果在函数内部有和函数外部同名变量,优先访问内部变量

def test1():

    x = 1

    def test2():

        x = 2

        def test3():                                                                                                                                     

            x = 3

            print(x)

        test3()

    test2()

test1()

结果:3

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

1. 无参数

Import time

def decorartor(func):            

    def wrapper():            

        start_time = time.time()

        func()        

        stop_time = time.time()

        print("the func run time is % s" % (stop_time - start_time))                                                

    return wrapper           

def test1():                   

    time.sleep(3)

    print('in the test1')

print(decorartor(test1))             

结果:<function decorartor.<locals>.wrapper at 0x000000000270AA60>

上面decorartor(test1) 得到的是 wrapper函数体存放的地址空间,想要进行执行并的到结果,可以如下操作:

test1 = decorartor(test1)

test1()

# 或decorartor(test1)()

结果:

in the test1                                                                                                                                             

the func run time is 3.0137999057769775

2.固定参数

import time

def decorartor(func):

    def wrapper(n):

        start_time = time.time()

        func(n)                                                                                                                                             

        stop_time = time.time()

        print("the func run time is % s" % (stop_time - start_time))

    return wrapper

def test1(n):

    time.sleep(3)

    print('in the test1 n is %s' % n)

#decorartor(test1)(10)

test = decorartor(test1)

test(10)

结果:

in the test1 n is 10

the func run time is 3.0138003826141357

3. 不定参数

import time                                                                                                                                             

def decorartor(func):

    def wrapper(*args, **kwargs):

        start_time = time.time()

        func(*args, **kwargs)

        stop_time = time.time()

        print("the func run time is % s" % (stop_time - start_time))

    return wrapper

def test1(n):

    time.sleep(3)

    print('in the test1 n is %s' % n)

def test2(n, m):

    time.sleep(3)

    print('in the test1 n is %s, m is %s' % (n, m))

decorartor(test1)(10)

decorartor(test2)(10, 100)

结果:

in the test1 n is 10

the func run time is 3.012200117111206

in the test1 n is 10, m is 100

the func run time is 3.000199794769287

4. 无参装饰器

import time

def decorartor(func):

    def wrapper(*args, **kwargs):                                                                                                        

        start_time = time.time()

        func(*args, **kwargs)

        stop_time = time.time()

        print("the func run time is % s" % (stop_time - start_time))

    return wrapper

@decorartor  # 相当于 test1 = decorartor(test1)

def test1():

    time.sleep(3)

    print('in the test1 ')

@decorartor  # 相当于 test2 = decorartor(test2)

def test2(n, m):

    time.sleep(3)

    print('in the test1 n is %s, m is %s' % (n, m))

test1()

test2(10, 100)

结果:

in the test1

the func run time is 3.0

in the test1 n is 10, m is 100

the func run time is 3.0

5. 有参装饰器

import time

def timer(timeout = 0):

    def decorartor(func):

        def wrapper(*args, **kwargs):

            start_time = time.time()

            func(*args, **kwargs)

            stop_time = time.time()                                                                                                            

            print("the func run time is % s" % (stop_time - start_time))

            if (stop_time - start_time) > timeout:

                print("程序运行超时")

        return wrapper

    return decorartor

@timer(2)        # 相当于 test1 = decorartor(test1)

def test1():

    time.sleep(3)

    print('in the test1')

@timer(5)          # 相当于 test2 = decorartor(test2)

def test2(n, m):

    time.sleep(3)

    print('in the test1 n is %s, m is %s' % (n, m))

test1()

test2(10, 100)

结果:

in the test1

the func run time is 3.0

程序运行超时

in the test1 n is 10, m is 100

the func run time is 3.0

 

原创粉丝点击