# -*- coding:utf-8 -*- # 带有通用参数的通用装饰器 def check_with_para(*args2, **kwargs2): def check(fn): def inner(*args, **kwargs): print("密码验证完毕,通过, args=%s,kwargs=%s"%(args, kwargs)) if "haha" == args2[0]: return fn(*args, **kwargs) elif "hehe" == args2[1]: print("这里可以做点其它不同的处理1") return fn(*args, **kwargs) else: print("这里可以做点其它不同的处理2") return fn(*args, **kwargs) return inner return check # 1. 先执行check_with_para("haha")函数, 返回check的引用 # 2. 再执行@check # 3. 使用@check对f1进行装饰 @check_with_para("haha") def f1(n1, n2): print("功能1执行中") return n1 + n2 @check_with_para("hehe", 1) def f2(n1, n2, n3): print("功能2执行中") return n1 - n2 * n3 @check_with_para("heihei", 2, True) def f3(): print("功能2执行中") if __name__ == '__main__': print(f1(7, 8)) print(f2(1, 4, 2)) print(f3())
来源:https://www.cnblogs.com/LiuYanYGZ/p/12169360.html