标签:%s alc 本质 else art log gen 工作 wrap
一 装饰器定义
装饰器:本质就是函数,一种增加函数或类的功能的简单方法,它可以快速的给不同的函数或者类插入相同的功能。
原则:(1)不修改被修饰函数的源代码。
(2)不修改被修饰函数的调用方式
装饰器 = 高阶函数+函数嵌套+闭包
#装饰器 = 高阶函数+函数嵌套+闭包 #高阶函数:输入或返回参数为函数 #函数嵌套中再定义函数 #闭包:函数中变量全部由函数内部提供 import time #为函数添加功能:计算函数的执行时间 def calc(num): res = 0; for i in range(num+1): res += i return res def timer(func): start_time = time.time() func(10000000) stop_time = time.time() print("运行时间为:",stop_time-start_time) # print(calc(10000)) timer(calc) ----------------------------------------------------------------------------- #定义装饰器timer 给函数增加打印执行时间功能 def timer(func): def wrapp(): start_time = time.time() ret = func(10000000) stop_time = time.time() print("运行时间为:",stop_time-start_time) return ret return wrapp @timer def calc(num): res = 0; for i in range(num+1): res += i return res # print(calc(10000)) # calc = timer(calc) print(calc()) #---------------------------------------------------------------------------- #函数可以传递参数 def timer(func): def wrapp(num): start_time = time.time() ret = func(num) stop_time = time.time() print("运行时间为:",stop_time-start_time) return ret return wrapp @timer def calc(num): res = 0; for i in range(num+1): res += i return res # print(calc(10000)) # calc = timer(calc) print(calc(10000)) #---------------------------------------------------------------------------- #装饰器函数的参数可变, def timer(func): def wrapp(*args,**kwargs): start_time = time.time() ret = func(*args,**kwargs) stop_time = time.time() print("运行时间为:",stop_time-start_time) return ret return wrapp @timer def calc(num): res = 0; for i in range(num+1): res += i return res @timer def infor(name,age,genel): print(name) print(age) print(genel) return "输出完成!" # print(calc(10000)) # calc = timer(calc) print(calc(10000)) print(infor(‘若水三千‘,28,‘男‘))
二 实例:增加验证功能
""" 给函数添加密码登录验证功能,一次同时给多个函数添加相同功能 """ def auto_vaild(func): def wrapper(*args,**kwargs): name = input("请输入用户名:") passwd = input("请输入与密码:") if name == "欢欢" and passwd == "123456": ret =func(*args,**kwargs) return ret else: print("您输入用户名或密码错误!!!") return wrapper @auto_vaild def index(): print("欢迎您来到京东商城!") @auto_vaild def payfor(name): print("%s您支付!"%name) @auto_vaild def collect(name): print("%s请你收藏好!"%name) return "欢迎下次购买" index() payfor("欢欢") print(collect("欢欢")) #------------------------------------------------------------------------- """ 给函数添加是否已经登录功能,若是已经登录,则不必再次登录 """ current_dict = {"userName":None,"Login":False} def auto_vaild(func): def wrapper(*args,**kwargs): if current_dict["userName"] != None and current_dict["Login"] != False: ret = func(*args,**kwargs) return ret name = input("请输入用户名:") passwd = input("请输入与密码:") if name == "欢欢" and passwd == "123456": current_dict["userName"] = name current_dict["Login"] = True ret =func(*args,**kwargs) return ret else: print("您输入用户名或密码错误!!!") return wrapper @auto_vaild def index(): print("欢迎您来到京东商城!") @auto_vaild def payfor(name): print("%s您支付!"%name) @auto_vaild def collect(name): print("%s请你收藏好!"%name) return "欢迎下次购买" index() payfor("欢欢") print(collect("欢欢")) #------------------------------------------------------------------------- """ 对用户进行登录进行验证,查看是否已经注册用户名,否则报错 """ current_dict = {"userName":None,"Login":False} user_dict =[{‘userName‘:"若水三千",‘passwd‘:123456}, {‘userName‘:"骑驴追车",‘passwd‘:654321}, {‘userName‘:"扬帆起航",‘passwd‘:135246}, {‘userName‘:"冷若冰霜",‘passwd‘:246135}] def auto_vaild(func): def wrapper(*args,**kwargs): if current_dict["userName"] != None and current_dict["Login"] != False: ret = func(*args,**kwargs) return ret name = input("请输入用户名:").strip() passwd = int(input("请输入与密码:").strip()) for user_login in user_dict: if user_login["userName"] == name and user_login["passwd"] == passwd: current_dict["userName"] = name current_dict["Login"] = True ret =func(*args,**kwargs) return ret else: #全部遍历循环完成没有合适的登录用户报错 print("您输入用户名或密码错误!!!") return wrapper @auto_vaild def index(): print("欢迎您来到京东商城!") @auto_vaild def payfor(name): print("%s您支付!"%name) @auto_vaild def collect(name): print("%s请你收藏好!"%name) return "欢迎下次购买" index() payfor("欢欢") print(collect("欢欢")) #------------------------------------------------------------------------- """ 给装饰器传递参数,根据不同的参数指向响应的功能! """ current_dict = {"userName":None,"Login":False} user_dict =[{‘userName‘:"若水三千",‘passwd‘:123456}, {‘userName‘:"骑驴追车",‘passwd‘:654321}, {‘userName‘:"扬帆起航",‘passwd‘:135246}, {‘userName‘:"冷若冰霜",‘passwd‘:246135}] def auto(auto_type = "filedb"): def auto_vaild(func): def wrapper(*args,**kwargs): print("认证类型",auto_type) if current_dict["userName"] != None and current_dict["Login"] != False: ret = func(*args,**kwargs) return ret name = input("请输入用户名:").strip() passwd = int(input("请输入与密码:").strip()) for user_login in user_dict: if user_login["userName"] == name and user_login["passwd"] == passwd: current_dict["userName"] = name current_dict["Login"] = True ret =func(*args,**kwargs) return ret else: #全部遍历循环完成没有合适的登录用户报错 print("您输入用户名或密码错误!!!") return wrapper return auto_vaild @auto(auto_type="filedb") def index(): print("欢迎您来到京东商城!") # @auto_vaild # def payfor(name): # print("%s您支付!"%name) # @auto_vaild # def collect(name): # print("%s请你收藏好!"%name) # return "欢迎下次购买" index() # payfor("欢欢") # print(collect("欢欢"))
三 嵌套装饰:插入两种不同类型的功能
import time def abc(fun): def wrapper(*args,**kwargs): print("函数开始工作!") ret = fun(*args,**kwargs) print("函数结束工作!") return ret return wrapper def timer(fun): def wrapper(*args,**kwargs): start_time = time.time() ret = fun(*args,**kwargs) stop_time = time.time() print("程序运行时间:%s"%(stop_time-start_time)) return ret return wrapper @abc @timer def calc(num): ret = 0 for i in range(num): ret += i return ret # print(calc(10000)) calc(100000)
标签:%s alc 本质 else art log gen 工作 wrap
原文地址:https://www.cnblogs.com/qilvzhuiche/p/9387682.html