day14——装饰器

day14

装饰器

装饰器本质就是闭包

开放封闭原则:

扩展是开放的(增加新功能),对源码是封闭的(修改已经实现的功能)

装饰器:用来装饰的工具

作用:在不改变源代码及调用方式的基础下额外增加新的功能

应用(面试的时候): 登录认证、日志

​ 增加一些功能

​ 校验用户登录的时候

​ 面向对象

语法糖 -- 甜:@+函数名

语法糖必须放在被装饰的函数正上方

# import time
# def times(f):
#     def inner():
#         start_time = time.time()
#         f()
#         print(time.time() - start_time)
#     return inner
# def foo():
#     time.sleep(1)
#     print('我是立业,我快!')
# foo = times(foo)
# foo()

# def wrapper(f):
#     def inner(*args,**kwargs):
#         f(*args,**kwargs)
#     return inner
# def func(*args,**kwargs):
#     print(f"这是{args,kwargs}函数")
# func = wrapper(func)
# func('alex','sir')
版一
# import time
# start_time = time.time()
# def func():
#     time.sleep(1)
#     print('我要飞')
# func()
# print(time.time() - start_time)
版二
# def times(f):
#     start_time = time.time()
#     f()
#     print(time.time() - start_time)
#
# def foo():
#     time.sleep(1)
#     print('小明')
#
# def func():
#     time.sleep(1)
#     print('章超印')
# s = func
# func = times
# func(s)

# s = foo
# foo = times
# foo(s)
版三
# def times(f):
#     def inner():
#         start_time = time.time()
#         f()
#         print(time.time() - start_time)
#     return inner
#
# def foo():
#     time.sleep(1)
#     print('小明')
# foo = times(foo)
# foo()

# def wrapper():
#     def inner():
#         print(1)
#     return inner
# wrapper()()

# def wrapper():
#     def inner(f):
#         print(f)
#     return inner
#
# def func():
#     print('小明')
# func = wrapper()
# func('alex')
版四
# def wrapper(f):
#     def inner(*args,**kwargs):
#         f(*args,**kwargs)
#     return inner
# def func(*args,**kwargs):
#     print(f'{args,kwargs}')
# func = wrapper(func)
# func(1,2,2,3,4,5,5,a=1,b=2)

# import time
# def wrapper(f):
#     def inner(*args,**kwargs):
#         start_time = time.time()
#         f(*args,**kwargs)
#         print((time.time() - start_time))
#     return inner
# def func(*args,**kwargs):
#     print(f'{args,kwargs}')
#     time.sleep(1)
# func = wrapper(func)
# func(1,2,3,4,54,5,a = 1, b =2)
版五
# import time
# def wrapper(f):
#     def inner(*args,**kwargs):
#         start_time = time.time()
#         print(f(*args,**kwargs))
#         return time.time() - start_time
#     return inner
# @wrapper
# def func(*args,**kwargs):
#     time.sleep(1)
#     print(f'{args,kwargs}')
#     return 'alex'
# @wrapper
# def foo(*args,**kwargs):
#     time.sleep(1)
#     print(f'{args,kwargs}')
# print(func(1,2,3,4,54,5,a = 1, b =2))
# print(foo(1523,2,541,5165,12,6,a='alex',b='eeeee'))
标准版
def func(a):  #a是要被装饰的函数名
    def foo(*args,**kwargs):
        "装饰之前的操作"
        ret = a(*args,**kwargs)
        "装饰之后的操作"
        return ret
    return foo
@func
def f1(*args,**kwargs):
    print(f"这是一个{args}")
    return "我可以返回了"
print(f1(1,2,3,34,4,5,a=1))
博客园
# login_dic = {
#     "username": None,
#     "flag": False
# }
# def auth(f):
#     def inner(*args,**kwargs):
#         if login_dic["flag"]:
#             return f()
#         else:
#             return login()
#     return inner
# @auth
# def index():
#     print(f"这是{login_dic['username']}主页")
#     return "主页没有内容"
# def login():
#     print("这是一个登录页面")
#     user = input("username:")
#     pwd = input("password:")
#     if user == "baoyuan" and pwd == "baoyuan123":
#         login_dic["flag"] = True
#         login_dic["username"] = user
#         return "登录成功!"
#     else:
#         return "用户名或密码错误!"
# @auth
# def comment():
#     print(f"这是{login_dic['username']}评论")
#
# while not login_dic["flag"]:
#     print(comment())
posted @ 2019-08-01 19:51  SupSnake  阅读(98)  评论(0编辑  收藏  举报