装饰器

"""

1、什么是装饰器

​ 器指的是工具/功能
​ 装饰指的是为被装饰对象添加额外的功能

​ 大白话:定义装饰器就是定义了一个函数,该函数就是用来为其他函数添加额外的功能的

2、为何要用装饰器

​ 程序的开发需要遵循一个原则:开放封闭原则
​ 开放:指的是对拓展功能开放
​ 封闭:指的是对修改源代码封闭

​ 装饰器就是在不修改被装饰对象源代码以及调用方式的前提为被装饰对象添加上新功能

3、如何用装饰器

"""

1、装饰器初探

'''

import time


def index(x, y):
    time.sleep(3)
    print('index===>', x, y)

index(1,2)


def home(name):
    time.sleep(2)
    print('home====>', name)

home("egon")

def outter():
    x = index

​    def wrapper(a, b):
​        start_time = time.time()
​        x(a, b)
​        stop_time = time.time()
​        print("run time is :%s" % (stop_time - start_time))

​    return wrapper  # 千万不要加括号


index = outter()  # f=wrapper

index(1, 2)  # wrapper()

'''

'''

2、装饰器改进1

#### 

​```
"""
import time


def index(x, y):
    time.sleep(3)
    print('index===>', x, y)

def home(name):
    time.sleep(2)
    print('home====>', name)

def outter(x):

x = home

​    def wrapper(*args,**kargs):
​        start_time = time.time()
​        x(*args,**kargs) # x("egon")
​        stop_time = time.time()
​        print("run time is :%s" % (stop_time - start_time))
​    return wrapper  # 千万不要加括号

index = outter(index)  # index=wrapper
home = outter(home)  # home=wrapper


index(1, 2)  # wrapper(1,2)
home("egon")  # wrapper("egon")
​```


"""

#### 

3、装饰器改进2

"""

import time


def index(x, y):
    time.sleep(3)
    print('index===>', x, y)
    return 456

def home(name):
    time.sleep(2)
    print('home====>', name)
    return 123


def outter(x):

x = home

​    def wrapper(*args, **kargs):
​        start_time = time.time()
​        res = x(*args, **kargs)  # x("egon")
​        stop_time = time.time()
​        print("run time is :%s" % (stop_time - start_time))
​        return res
​    return wrapper  # 千万不要加括号

index = outter(index)  # index=wrapper
home = outter(home)  # home=wrapper

res1 = index(1, 2)  # wrapper(1,2)
res2 = home("egon")  # wrapper("egon")
print("====>",res1)
print("====>",res2)

"""

4、语法糖

"""
import time


def outter(x):

x = home

​    def wrapper(*args, **kargs):
​        start_time = time.time()
​        res = x(*args, **kargs)  # x("egon")
​        stop_time = time.time()
​        print("run time is :%s" % (stop_time - start_time))
​        return res

​    return wrapper  # 千万不要加括号


@outter  # index = outter(index)  # index=wrapper
def index(x, y):
    time.sleep(3)
    print('index===>', x, y)
    return 456


@outter  # home = outter(home)  # home=wrapper
def home(name):
    time.sleep(2)
    print('home====>', name)
    return 123


res1 = index(1, 2)  # wrapper(1,2)
res2 = home("egon")  # wrapper("egon")
print("====>", res1)
print("====>", res2)
"""
import time

#### 5、装饰器的模板

"""
def outter(func):
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        return res
    return wrapper

def timmer(func):
    def wrapper(*args, **kwargs):
        start=time.time()
        res = func(*args, **kwargs)
        stop=time.time()
        print(stop -start)
        return res
    return wrapper

def auth(func):
    def wrapper(*args, **kwargs):
        name = input("请输入您的账号:").strip()
        pwd = input("请输入您的账号:").strip()
        if name == "egon" and pwd == "123":
            print('登录成功')
            res = func(*args, **kwargs)
            return res
        else:
            print("账号密码错误")
    return wrapper

@auth
def index(x, y):
    time.sleep(1)
    print('index===>', x, y)
    return 456

@auth
def home(name):
    time.sleep(0.5)
    print('home====>', name)
    return 123

index(1,2)
home("egon")
"""

4、wraps

'''
from functools import wraps

def timmer(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        start=time.time()
        res = func(*args, **kwargs)
        stop=time.time()
        print(stop -start)
        return res

wrapper.__name__ = func.__name__

wrapper.__doc__ = func.__doc__

​    return wrapper

@timmer # index=timmer(index) # index=wrapper
def index(x, y):
    """
    这是index函数的文档注释
    """
    time.sleep(1)
    print('index===>', x, y)
    return 456

index(1,2)

print(index.__name__)

help(index)
'''

5.最完整的模板

from functools import wraps

def outter(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        return res

​    return wrapper
posted @ 2020-06-18 13:18  刘海子  阅读(96)  评论(0编辑  收藏  举报