zoukankan      html  css  js  c++  java
  • 2018.09.28python学习第十三天part1

    # part 1
    # 叠加多个装饰器的顺序

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

    # eg.1

    import time
    def timmer(func):
    def wrapper1(*args,**kwargs):
    print('welcome to wrapper1 page')
    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 inner(func):
    def wrapper2(*args,**kwargs):
    print('wrapper2 is running')
    name=input('username: ').strip()
    pwd=input('password: ').strip()
    if engine == 'file':
    print('基于文件认证')
    if name == 'egon'and pwd=='123':
    print('login success')
    res=func(*args,**kwargs)
    return res
    elif engine == 'mysql':
    print('基于mysql认证')
    elif engine == 'ldap':
    print('基于ldap认证')
    else:
    print('错误认证')
    return wrapper2
    return inner


    @auth(engine='file')###可以在这里更改@auth(engine='file')和@timmer的位置,发现print出来的内容和时间都不一样
    @timmer
    def index():
    print('welcome to index page')
    time.sleep(2)
    index()
  • 相关阅读:
    Python 的with关键字
    java解析xml
    Java IO & Serialization
    Java动态编译
    爬虫下载City Scape数据
    Pytorch多GPU训练
    可视化利器Visdom
    GLOG使用Demo
    hyperopt自动调参
    [Redis源码阅读]redis持久化
  • 原文地址:https://www.cnblogs.com/hello-yuanjing/p/9720594.html
Copyright © 2011-2022 走看看