这里做个最简单的账号密码验证,主要为了实现装饰器同时对多个函数起作用
1 FLAG = False #加一个变量防止多次登录验证 2 def wrapper(f): 3 def inner(*args,**kwargs): 4 ‘‘‘登录程序‘‘‘ 5 global FLAG #将FLAG变量设置成全局变量 6 if FLAG: 7 ret = f(*args, **kwargs) 8 return ret 9 else: 10 username = input(‘username:‘) 11 password = input(‘password:‘) 12 if username == ‘123‘ and password == ‘345‘: 13 FLAG = True 14 ret = f(*args,**kwargs) 15 return ret 16 else: 17 print(‘登录失败‘) 18 return inner 19 20 @wrapper 21 def func_1(): 22 print(‘func1 is running!‘) 23 24 @wrapper 25 def func_2(): 26 print(‘func2 is running!‘) 27 28 func_1() 29 func_2()
运行结果:
1 username:123 2 password:345 3 func1 is running! 4 func2 is running!
原文地址:https://www.cnblogs.com/shuimohei/p/9680206.html
时间: 2024-10-08 07:26:21