• python 阿狸的进阶之路(4)


    装饰器

    #1、开放封闭原则:对扩展开放,对修改是封闭
    #2、装饰器:装饰它人的,器指的是任意可调用对象,现在的场景装饰器-》函数,被装饰的对象也是-》函数
    #原则:1、不修改被装饰对象的源代码 2、不修改被装饰对象的调用方式
    #装饰器的目的:在遵循1,2的前提下为被装饰对象添加上新功能

     (1)无参数类型

    import time
    def outer(func):
        def inner():
            time.sleep(1)
            print("hello")
            func()
        return inner

    def bar(): print('world')

     (2)有参数类型

    # 有参装饰器
    import time
    
    def auth2(engine='file'):
        def auth(func): # func=index
            def inner(*args,**kwargs):
                if engine == 'file':
                    name=input('name>>: ').strip()
                    password=input('password>>: ').strip()
                    if name == 'egon' and password == '123':
                        print('login successful')
                        return func(*args,**kwargs)
                    else:
                        print('login err')
                elif engine == 'mysql':
                    print('mysql auth')
                elif engine == 'ldap':
                    print('ldap auth')
                else:
                    print('engin not exists')
            return inner
        return auth
    
    @auth2(engine='mysql') #@auth #index=auth(index) #index=inner
    def index(name):
        time.sleep(1)
        print('welecome %s to index' %name)
        return 1111
    
    res=index('egon') #res=inner('egon')
    print(res)

    (3)并列装饰器

    import time
    def timmer(func):
        def inner(*args,**kwargs):
            start=time.time()
            res=func(*args,**kwargs)
            stop=time.time()
            print('run time is %s' %(stop-start))
            return res
        return inner
    
    def auth2(engine='file'):
        def auth(func): # func=index
            def inner(*args,**kwargs):    # 一致
                if engine == 'file':
                    name=input('name>>: ').strip()
                    password=input('password>>: ').strip()
                    if name == 'egon' and password == '123':
                        print('login successful')
                        res = func(*args,**kwargs) #一致
                        return res
                    else:
                        print('login err')
                elif engine == 'mysql':
                    print('mysql auth')
                elif engine == 'ldap':
                    print('ldap auth')
                else:
                    print('engin not exists')
            return inner
        return auth
    
    
    @auth2(engine='file')
    @timmer
    def index(name):
        time.sleep(1)
        print('welecome %s to index' %name)
        return 1111
    
    res=index('egon')
    print(res)

    (4)

    from functools import wraps
    import time
    def timmer(func):
        @wraps(func)
        def inner(*args,**kwargs):
            start=time.time()
            res=func(*args,**kwargs)
            stop=time.time()
            print('run time is %s' %(stop-start))
            return res
        # inner.__doc__=func.__doc__
        # inner.__name__=func.__name__
        return inner
    
    @timmer
    def index(name): #index=inner
        '''index 函数。。。。。'''
        time.sleep(1)
        print('welecome %s to index' %name)
        return 1111
    
    res=index('egon')
    print(res)
    
    print(help(index))
  • 相关阅读:
    自定义事件的触发dispatchEvent
    [转]ProxmoxVE 干掉 VMware
    【转】怎么去阅读Chromium的源码?
    Delphi内存专题
    Delphi 线程同步技术(转)
    【纸模】六角大王 Super 5.6 CHS 简体中文版 U20080725+[手册]窗口与工具的概要(PDF格式)
    ReSharper反编译C#类库
    CS DevExpress程序启动(主窗体初始化优化)
    【EasyNetQ】- 发布/订阅模式
    Intellij Idea调试java文件时 怎么跳过class文件?
  • 原文地址:https://www.cnblogs.com/taozizainali/p/8202361.html
Copyright © 2020-2023  润新知