zoukankan      html  css  js  c++  java
  • 叠加多个装饰器

    # _*_ coding: utf-8 _*_

    # 加载装饰器就是将原函数名偷梁换柱成了装饰器最内层那个wrapper函数
    # 在加载完毕,调用原函数其实就是在调用wrapper函数

    # 当一个被装饰的对象同时叠加多个装饰器时
    # 装饰器的加载顺序是:自下而上
    # 装饰器内wrapper的函数的执行顺序是:自上而下

    import time


    def timmer(func):

    def wrapper1(*args, **kwargs):
    print('===================================>wrapper1运行了')
    start=time.time()
    res = func(*args, **kwargs)
    stop=time.time()
    print('run time is %s' %(stop - start))
    return res
    return wrapper1

    def auth(engine='file'):
    def xxx(func): # func = 原来的index内存地址
    def wrapper2(*args, **kwargs):
    print('===================================>wrapper2运行了')
    name=input('username>>>: ').strip()
    pwd=input('password>>>: ').strip()
    if engine == 'file': #去外层找
    print('基于文件的认证')
    if name == 'egon' and pwd == '123':
    print('login successfull')
    res = func(*args, **kwargs)
    return res
    elif engine == 'mysql':
    print('基于mysql的认证')
    elif engine == 'ldap':
    print('基于ldap的认证')
    else:
    print('错误的认证源')
    return wrapper2
    return xxx

    # 自上而下 运行
    # 自下而上 加载
    @auth(engine='file')
    @timmer
    def index():
    print('welcome to index page')
    time.sleep(2)
    return 123
    index()
    # res = index()
    # print(res)
  • 相关阅读:
    获取 鼠标 键盘 最后响应信息
    EF 6 for mysql
    Python中的OS模块。
    python中的随机函数random的用法示例random
    Python中OSI七层模型
    Pythoon中的面向对象三大特征
    Python中的函数(学习日记)
    Python的6种运算符(日记)
    计算机的发展史
    osi七层
  • 原文地址:https://www.cnblogs.com/OutOfControl/p/9720904.html
Copyright © 2011-2022 走看看