15-python-decorators

# 装饰器:装饰、装修
# 开放封闭原则:对代码的扩展开放;对源码的修改是封闭的。
# 装饰器:在不改变原函数代码以及调用方式的前提下为其增加新的功能。

import time
# print(time.time()) # 格林威治时间

# round 1
# 如果测试别人的代码必须重新赋值。
# def index():
# time.sleep(2)
# print('欢迎登录youtube')
#
# start_time = time.time()
# index()
# end_time = time.time()
# print(end_time-start_time)
#
# def index2():
# time.sleep(2)
# print('欢迎登录youtube')
#
# start_time = time.time()
# index2()
# end_time = time.time()
# print(end_time-start_time)

# round 2
# 利用函数解决代码重复问题
# index源码没变,给原函数添加了一个新的功能测试原函数的执行效率的功能,但是改变了原函数的调用方式
# def index():
# time.sleep(2)
# print('欢迎登录youtube')
#
# def index2():
# time.sleep(2)
# print('欢迎登录youtube2')
#
# def timer(func):
# start_time = time.time()
# func()
# end_time = time.time()
# print(end_time-start_time)

# timer(index2)

# round 3
# 不改变原函数的调用方式
# def index():
# time.sleep(2)
# print('欢迎登录youtube')
#
# def index2():
# time.sleep(2)
# print('欢迎登录youtube2')
#
# def timer(func):
# def inner():
# start_time = time.time()
# func()
# end_time = time.time()
# print(end_time-start_time)
# return inner
# index = timer(index)
# index2 = timer(index2)
# index()
# index2()

# round 4
# python做了一个优化,提出了一个语法糖的概念
# def timer(func):
# def inner():
# start_time = time.time()
# func()
# end_time = time.time()
# print(end_time-start_time)
# return inner
#
# @timer
# def index():
# time.sleep(2)
# print('欢迎登录youtube')
# @timer
#
# def index2():
# time.sleep(2)
# print('欢迎登录youtube2')
#
# index()
# index2()

# round 5
# 被装饰函数带返回值
# ret获取inner中的func的返回值
# def timer(func):
# def inner():
# start_time = time.time()
# ret = func()
# end_time = time.time()
# print(end_time-start_time)
# return ret
# return inner
#
# @timer
# def index():
# time.sleep(2)
# print('欢迎登录youtube')
# return 666
#
# ret = index()
# print(ret)

# round 6
# 被装饰函数带返回值
# ret获取inner中的func的返回值
# def timer(func):
# def inner(n):
# start_time = time.time()
# ret = func(n)
# end_time = time.time()
# print(end_time-start_time)
# return ret
# return inner
#
# @timer
# def index(a):
# time.sleep(2)
# print(f'欢迎{a}登录youtube')
# return 666
#
# ret = index('hello')
# print(ret)

# round 7
# 标准版装饰器
# 格式:
# def wrapper():
# def inner(f):
# ret = f(*args,**kwargs)
# return ret
# return inner

# def timer(func):
# def inner(*args, **kwargs):
# start_time = time.time()
# ret = func(*args, **kwargs)
# end_time = time.time()
# print(end_time-start_time)
# return ret
# return inner
#
# @timer
# def index(*args, **kwargs):
# time.sleep(2)
# print(f'欢迎{args}登录youtube')
# return 666
#
# ret = index('hello')
# print(ret)

# 装饰器的应用:登录认证
# status_dict = {
# 'name': None,
# 'status': False,
# }
#
# def login():
# print('login')
#
# def auth(f):
# def inner(*args, **kwargs):
# if status_dict['status']:
# ret = f(*args, **kwargs)
# return ret
# else:
# username = input('name: ').strip()
# password = input('pass: ').strip()
# if username == 'dfg' and password == '123':
# print('login success')
# status_dict['name'] = username
# status_dict['status'] = True
# ret = f(*args, **kwargs)
# return ret
# else:
# print('login fail')
# return inner
#
# def register():
# print('register')
#
# @auth
# def article():
# print('article')
# @auth
# def comment():
# print('comment')
# @auth
# def dariy():
# print('dariy')
#
# article()
# comment()
# dariy()
上一篇:webpack 配置react脚手架(五):mobx


下一篇:Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option, whose