zoukankan      html  css  js  c++  java
  • Python__装饰器

    开放封闭原则:对拓展是开放的,对修改是封闭的

    装饰器:装饰他人的工具,装饰的目的是为他人添加新功能

    装饰器本身是任意可调用对象,被装饰的对象本身也可以是任意可调用的对象

    装饰器遵循的原则:1、不修改被装饰对象的源代码

             2、不修改被调用对象的调用方式

    装饰器的目的:在遵循1和2原则的前提下,为其他函数添加新功能

    @装饰器名:必须写在被装饰对象的正上方,且是单独一行

    #Author wangmengzhu
    import time
    def timmer(func):
        def wrapper():
            start = time.time()
            func()
            stop = time.time()
            print('run time is %s '%(stop - start))
        return wrapper
    @timmer
    def index():
        time.sleep(3)
        print('welcome to index')
    index()

    import time
    def timmer(func):
    def wrapper(*args,**kwargs):##对于有参和无参都可以传入
    start = time.time()
    res = func(*args,**kwargs)
    stop = time.time()
    print('run time is %s '%(stop - start))
    return res
    return wrapper
    @timmer
    def index(name):
    time.sleep(3)
    print('%s welcome to index'%(name))
    return 'lala'
    res = index('susu')
    print(res)


    有参装饰器

    current_user = {'user':None}
    def auth(func):
        def wrapper(*args,**kwargs):
            if current_user['user']:
                res = func(*args, **kwargs)
                return res
            else:
                name = input('请输入你的用户名>>: ').strip()
                password = input('请输入你的密码>>: ').strip()
                with open('db.txt', encoding='utf-8') as f:
                    info = f.read()
                    info_dic = eval(info)
                if name in info_dic and password == info_dic[name]:
                    res = func(*args, **kwargs)
                    current_user['user'] = name
                    return res
                else:
                    print('name or password not exists')
        return wrapper
    @auth
    def index():
        print('from index')
    @auth
    def foo(name):
        print('%s welcome to this page'%(name))
    index()
    foo('egon')
    current_user = {'user':None}
    def deco(auth_type = 'file'):
        def auth(func):
            def wrapper(*args,**kwargs):
                if auth_type == 'file':
                    if current_user['user']:
                        res = func(*args, **kwargs)
                        return res
                    else:
                        name = input('请输入你的用户名>>: ').strip()
                        password = input('请输入你的密码>>: ').strip()
                        with open('db.txt', encoding='utf-8') as f:
                            info = f.read()
                            info_dic = eval(info)
                        if name in info_dic and password == info_dic[name]:
                            res = func(*args, **kwargs)
                            current_user['user'] = name
                            return res
                        else:
                            print('name or password not exists')
                elif auth_type == 'mysql':
                    pass
                else:
                    print('not valid')
            return wrapper
        return auth
    @deco()
    def index():
        print('from index')
    @deco()
    def foo(name):
        print('%s welcome to this page'%(name))
    index()
    foo('egon')

     装饰器补充

    import time
    from functools import wraps
    def timmer(func):
        @wraps(func)
        def wrapper(*args,**kwargs):##对于有参和无参都可以传入
            start = time.time()
            res = func(*args,**kwargs)
            stop = time.time()
            print('run time is %s '%(stop - start))
            return res
        return wrapper
    @timmer
    def index(name):
        '''
        这是index函数
        '''
        time.sleep(3)
        print('%s welcome to index'%(name))
        return 'lala'
    index('susu')
    
    print(index.__doc__)#查看注释信息
    print(help(index))
  • 相关阅读:
    hdu 4302 Holedox Eating 夜
    poj 1947 Rebuilding Roads 夜
    hdu 4303 Hourai Jeweled 夜
    poj 1286 Necklace of Beads 夜
    poj 2057 The Lost House 夜
    hdu 4301 Divide Chocolate 夜
    poj 3140 Contestants Division 夜
    BOM(二)
    Str的方法
    求出字符窜的字母的个数
  • 原文地址:https://www.cnblogs.com/wangmengzhu/p/7230036.html
Copyright © 2011-2022 走看看