1.用户登录认证
- 有三个界面需要登录认证通过后才能查看
- 一个登录函数作为装饰器
- 调用使用@函数名
- 调用中如果加参数,则装饰器需要再加一层
1 def login(func): 2 def desc(*args,**kwargs): 3 user='test' 4 passwd ='123456' 5 n=0 6 if n<=3: 7 for i in range(3): 8 user_input=input('用户名') 9 passwd_input=input('密码') 10 if user_input==user and passwd_input==passwd: 11 func(*args,**kwargs) 12 break 13 else: 14 print('输入错误,请重新输入') 15 n+=1 16 return desc 17 18 @login 19 def home(): 20 print('welcome to home') 21 @login 22 def product(): 23 print('welcome to product') 24 @login 25 def solution(): 26 print('welcome to solution') 27 28 home() 29 product() 30 solution()View Code
2.日志操作时间
- 在三个界面记录操作时间
- 使用logger日志
1 def logger(func): 2 '''日志装饰器''' 3 def desc(*args,**kwargs): 4 start_time=time.time() 5 func() 6 end_time=time.time() 7 print('操作了%s时间'%(end_time-start_time)) 8 return desc 9 @logger 10 def home(*args,**kwargs): 11 time.sleep(2) 12 print('welcome to home') 13 @logger 14 def product(): 15 time.sleep(2) 16 print('welcome to product') 17 @logger 18 def solution(): 19 time.sleep(2) 20 print('welcome to solution') 21 22 home() 23 product() 24 solution()View Code 标签:场景,python,welcome,time,print,home,logger,装饰,def From: https://www.cnblogs.com/Little-Girl/p/17987765