day15——有参装饰器、多个装饰器装饰一个函数

day15

装饰器的进阶

有参装饰器

@auth(chose) 相等于以下两行代码的解构

wrapper = auth(chose)

foo = wrapper(foo)

# dic = {'username': None,
#        'flag': False
#        }
#
# def auth(argv):
#     def wrapper(f):
#         def inner(*args, **kwargs):
#             if dic['flag']:
#                 f(*args, **kwargs)
#             else:
#                 if argv == "QQ":
#                     print('欢迎来到QQ')
#                     user = input("username:")
#                     pwd = input("password:")
#                     if user == 'alex' and pwd == 'alex123':
#                         dic['flag'] = True
#                         dic['username'] = user
#                         f(*args, **kwargs)
#                     else:
#                         print('用户名或者密码错误!')
#                 elif argv == '微信':
#                     print('欢迎来到微信')
#                     user = input("username:")
#                     pwd = input("password:")
#                     if user == 'alex' and pwd == 'alex123':
#                         dic['flag'] = True
#                         dic['username'] = user
#                         f(*args, **kwargs)
#                     else:
#                         print('用户名或者密码错误!')
#                 elif argv == '抖音':
#                     print('欢迎来到抖音')
#                     user = input("username:")
#                     pwd = input("password:")
#                     if user == 'alex' and pwd == 'alex123':
#                         dic['flag'] = True
#                         dic['username'] = user
#                         f(*args, **kwargs)
#                     else:
#                         print('用户名或者密码错误!')
#                 else:
#                     print('欢迎来到邮箱')
#                     user = input("username:")
#                     pwd = input("password:")
#                     if user == 'alex' and pwd == 'alex123':
#                         dic['flag'] = True
#                         dic['username'] = user
#                         f(*args, **kwargs)
#                     else:
#                         print('用户名或者密码错误!')
#         return inner
#     return wrapper
# msg = """
# QQ
# 微信
# 抖音
# 邮箱
# 请输入您要选择登陆的app:
# """
# chose = input(msg)
# @auth(chose.upper())
# def func():
#     print(f'{chose}欢迎你')
# func()

# @auth(chose) 相等于以下两行代码的解构
# wrapper = auth(chose)
# foo = wrapper(foo)
多个装饰器装饰一个函数

被装饰的函数正上方多个装饰器,先执行离被装饰函数最近的装饰器

def wrapper1(f):
    def inner1():
        print(1)
        f()
        print(2)
    return inner1

def wrapper2(f):
    def inner2():
        print(2)
        f()
        print(3)
    return inner2

def wrapper3(f):
    def inner3():
        print(3)
        f()
        print(4)
    return inner3

@wrapper1  
@wrapper2  
@wrapper3  
def foo():
    print(222)
foo()      
# 1 2 3 222 4 3 2
# foo = wrapper3(foo)  # foo == inner3
# foo = wrapper2(foo)  # inner2 = wrapper2(inner3)
# foo = wrapper1(foo)  # inner1 = wrapper1(inner2)
# foo()				   #inner1

def wrapper1(f):
    def inner1():
        print(1)
        f()
        print(2)
    return inner1

def wrapper2(f):
    def inner2():
        print(2)
        f()
        print(3)
    return inner2

def wrapper3(f):
    def inner3():
        print(3)
        f()
        print(4)
    return inner3

@wrapper1  
@wrapper3  
@wrapper2  
def foo():
    print(222)
foo()      
# 1 3 2 222 3 4 2
# foo = wrapper2(foo)  # foo == inner2
# foo = wrapper3(foo)  # inner3 = wrapper3(inner2)
# foo = wrapper1(foo)  # inner1 = wrapper1(inner3)
# foo()				   #inner1
posted @ 2019-08-01 19:52  SupSnake  阅读(98)  评论(0编辑  收藏  举报