导图社区 一张图,一个案例,看懂python有参装饰器
python有参装饰器,需求思路: 在下面代码所显示无参装饰器的基础上, 如果需要外界数据/数据库在wrapper函数内做对比,判断,计算,由于 1. wrapper(*args,**kwargs)函数参数直接传值func 2. outer函数,由于语法糖的限制不能接受多个参
编辑于2023-09-28 19:28:58 北京市PMP项目管理全过程解析:掌握核心,高效推进! 本文深入解析PMP项目管理的五大过程组和十大知识领域,涵盖启动、规划、执行、监控和收尾全过程。从整合管理到范围、进度、成本、质量、资源、沟通、风险、采购和相关方管理。
一张图,一个案例,看懂python装饰器,此图基于林海峰egon老师的讲解制成,感谢跨时跨空的林老师,讲解真的很棒。
python有参装饰器,需求思路: 在下面代码所显示无参装饰器的基础上, 如果需要外界数据/数据库在wrapper函数内做对比,判断,计算,由于 1. wrapper(*args,**kwargs)函数参数直接传值func 2. outer函数,由于语法糖的限制不能接受多个参
社区模板帮助中心,点此进入>>
PMP项目管理全过程解析:掌握核心,高效推进! 本文深入解析PMP项目管理的五大过程组和十大知识领域,涵盖启动、规划、执行、监控和收尾全过程。从整合管理到范围、进度、成本、质量、资源、沟通、风险、采购和相关方管理。
一张图,一个案例,看懂python装饰器,此图基于林海峰egon老师的讲解制成,感谢跨时跨空的林老师,讲解真的很棒。
python有参装饰器,需求思路: 在下面代码所显示无参装饰器的基础上, 如果需要外界数据/数据库在wrapper函数内做对比,判断,计算,由于 1. wrapper(*args,**kwargs)函数参数直接传值func 2. outer函数,由于语法糖的限制不能接受多个参
需求思路 在下面代码所显示无参装饰器的基础上, 如果需要外界数据/数据库在wrapper函数内做对比,判断,计算,由于 1. wrapper(*args,**kwargs)函数参数直接传值func 2. outer函数,由于语法糖的限制不能接受多个参数 暂时无法传入更多参数,在此基础上优化如下 def outer(func) def wrapper(*args,**kwargs) res=func(*args,**kwargs) return res @outer def function(*args,**kwargs) function(1,2) 在角落继续表达对跨时空的Egon林海峰老师绵延不断的崇拜之感
案例:需求为在每个被装饰器装饰的函数执行前验证名称密码 def f1(func): def wrapper(*args, **kwargs): name=input("your name: ") pwd=input("your password: ") if name=="tom" and pwd=="123": print("correct name and password! ") res=func(*args,**kwargs) #需求为此处根据不同用户的来源,给出不同的欢迎语, #如source==students 欢迎tom同学,source=company, 欢迎同事tom, #source==teachers, 欢迎tom老师 else: print("wrong name or password!") return res return wrapper @f1 def index(*args,**kwargs): print("index function output is ",*args,**kwargs) return "the index1 function is done" @f1 def index2(*args,**kwargs): print("index2 function output is ",*args,**kwargs) return "the index2 function is done" index(1,2)
优化1: 不使用语法糖,在f1中传入多个参数 def f1(func,source): def wrapper(*args, **kwargs): name=input("your name: ") pwd=input("your password: ") if name=="tom" and pwd=="123": print("correct name and password! ") if source=="teachers": print("欢迎{}老师!".format(name)) elif source=="students": print("欢迎{}同学!".format(name)) elif source=="company employees": print("欢迎{}同事!".format(name)) else: print("please choose proper source") res=func(*args,**kwargs) else: print("wrong name or password!") return res return wrapper def index(*args,**kwargs): print("index function output is ",*args,**kwargs) return "the index1 function is done" def index2(*args,**kwargs): print("index2 function output is ",*args,**kwargs) return "the index2 function is done" def index3(*args,**kwargs): print("index3 function output is ",*args,**kwargs) return "the index2 function is done" index=f1(index,"teachers") index2=f1(index2,"students") index3=f1(index3,"company employees") index2(1,2 ) '''output: your name: tom your password: 123 correct name and password! 欢迎tom同学! index2 function output is 1 2'''
优化2: 回归语法糖,在f1和wrapper外面继续套入函数, 通过在装饰器函数 def source_auth(source): def f1(func): def wrapper(*args, **kwargs): name=input("your name: ") pwd=input("your password: ") if name=="tom" and pwd=="123": print("correct name and password! ") if source=="teachers": print("欢迎{}老师!".format(name)) elif source=="students": print("欢迎{}同学!".format(name)) elif source=="company employees": print("欢迎{}同事!".format(name)) else: print("please choose proper source") res=func(*args,**kwargs) else: print("wrong name or password!") return res return wrapper return f1 f1=source_auth(source="teachers") @f1 def index(*args,**kwargs): print("index function output is ",*args,**kwargs) return "the index1 function is done" f1=source_auth(source="students") @f1 def index2(*args,**kwargs): print("index2 function output is ",*args,**kwargs) return "the index2 function is done" f1=source_auth(source="company employees") @f1 def index3(*args,**kwargs): print("index3 function output is ",*args,**kwargs) return "the index2 function is done" # index=f1(index,"teachers") # index2=f1(index2,"students") # index3=f1(index3,"company employees") index2(1,2 ) '''output: your name: tom your password: 123 correct name and password! 欢迎tom同学! index2 function output is 1 2'''
优化3:继续精简 def source_auth(source): def f1(func): def wrapper(*args, **kwargs): name=input("your name: ") pwd=input("your password: ") if name=="tom" and pwd=="123": print("correct name and password! ") if source=="teachers": print("欢迎{}老师!".format(name)) elif source=="students": print("欢迎{}同学!".format(name)) elif source=="company employees": print("欢迎{}同事!".format(name)) else: print("please choose proper source") res=func(*args,**kwargs) else: print("wrong name or password!") return res return wrapper return f1 @source_auth(source="teachers") # 此处,也可输入多个变量 def index(*args,**kwargs): print("index function output is ",*args,**kwargs) return "the index1 function is done" @source_auth(source="students") def index2(*args,**kwargs): print("index2 function output is ",*args,**kwargs) return "the index2 function is done" @source_auth(source="company employees") def index3(*args,**kwargs): print("index3 function output is ",*args,**kwargs) return "the index2 function is done" index2(1,2 ) '''output: your name: tom your password: 123 correct name and password! 欢迎tom同学! index2 function output is 1 2'''