首页 > 其他 > 详细

装饰器的修订

时间:2019-12-03 21:35:58      阅读:62      评论:0      收藏:0      [点我收藏+]
# import time
#
# def index():
# time.sleep(3)
# print(‘Welcome to China‘)
# def timmer(func):
# def inner():
# start = time.time()
# func()
# stop = time.time()
# print(‘run time is %s‘ % (stop - start))
# return inner
#
# index=timmer(index)
# index()
‘‘‘
Welcome to China
run time is 3.0002970695495605
‘‘‘
# 装饰器语法:在被装饰对象正上方一行单独写上@装饰器名
# import time
# def timmer(func):
# def inner():
# start = time.time()
# func()
# stop = time.time()
# print(‘run time is %s‘ % (stop - start))
# return inner
# @timmer # 把@装饰器正下方的函数名当作参数传给@装饰器名,并把结果重新命名给index
# def index():
# time.sleep(3)
# print(‘Welcome to China‘)
#
# index()
‘‘‘
Welcome to China
run time is 3.000959634780884
‘‘‘
# import time
# def timmer(func):
# def inner():
# start = time.time()
# res=func()
# stop = time.time()
# print(‘run time is %s‘ % (stop - start))
# return res
# return inner
# @timmer
# def index():
# time.sleep(1)
# print(‘Welcome to China‘)
# return 521
#
# res=index()
# print(res)
‘‘‘
Welcome to China
run time is 1.0006263256072998
521
‘‘‘
# import time
# def timmer(func):
# def inner(*args,**kwargs):
# start = time.time()
# res=func(*args,**kwargs)
# stop = time.time()
# print(‘run time is %s‘ % (stop - start))
# return res
# return inner
# @timmer
# def home(name):
# print(‘Welcome %s to China‘%name)
#
# home(‘Rambo‘)
‘‘‘
Welcome Rambo to China
run time is 0.0
‘‘‘
# 有参装饰器
# import time
# def auth(func):
# # func=index
# def inner(*args,**kwargs):
# name=input(‘>>:name‘).strip()
# password=input(">>:paseword").strip()
# if name == "Rambo" and password == ‘123‘:
# print(‘登录成功‘)
# return func(*args,**kwargs)
# else:
# print(‘用户名或者密码错误‘)
# return inner
# @auth
# def index(name):
# time.sleep(1)
# print(‘Welcome %s to China‘%name)
# return 521
#
# res=index(‘Rambo‘)
# print(res)

装饰器的修订

原文:https://www.cnblogs.com/0B0S/p/11979341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!