码迷,mamicode.com
首页 > 编程语言 > 详细

15-python-decorators

时间:2020-05-03 14:51:28      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:use   python   优化   pass   reg   应用   end   time()   装饰器   

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

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()

15-python-decorators

标签:use   python   优化   pass   reg   应用   end   time()   装饰器   

原文地址:https://www.cnblogs.com/Daspig/p/12821740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!