多层装饰器叠加

多层装饰器叠加

import time


def statistic_time(func):
    """统计时间的装饰器"""
    def wrapper2(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        print('run time:{}'.format((end - start)), '/s')
        return res

    return wrapper2


def login(func):
    """登录验证功能的装饰器"""
    def wrapper(*args, **kwargs):
        u_name = input('Enter a user name>>:')
        u_pwd = input('Enter a password>>:')
        if u_name == 'poco' and u_pwd == '123':
            print('Welcome!', u_name)
        else:
            print('User name or password error!')
        res = func(*args, **kwargs)
        return res

    return wrapper


@statistic_time
@login
def index():
    time.sleep(1)
    print('====Xiao Bi Zai zi OS====')


index()
import time


def statistic_time(func):
    """统计时间的装饰器"""
    def wrapper1(*args, **kwargs):
        start = time.time()
        res = func(*args, **kwargs)
        end = time.time()
        print('run time:{}'.format((end - start)), '/s')
        return res

    return wrapper1


def login(func):
    """登录验证功能的装饰器"""
    def wrapper2(*args, **kwargs):
        u_name = input('Enter a user name>>:')
        u_pwd = input('Enter a password>>:')
        if u_name == 'poco' and u_pwd == '123':
            print('Welcome!', u_name)
        else:
            print('User name or password error!')
        res = func(*args, **kwargs)
        return res

    return wrapper2


@statistic_time
@login
def index():
    time.sleep(1)
    print('====Xiao Bi Zai zi OS====')


index()
总结:多个装饰器的

加载顺序是自下而上
执行(运行)顺序是自上而下


posted @ 2021-04-07 18:56  黑影Poco  阅读(32)  评论(0)    收藏  举报