#编写一个装饰器,为多个函数加上认证功能 #要求登录一次成功之后,后续函数不需要登录 s = False def login(func): def inner(*args,**kwargs): global s if s: ret = func(*args, **kwargs) return ret else: username = input(‘请输入用户名:‘) passwd = input(‘请输入密码:‘) if username == ‘taibai‘ and passwd == ‘22222‘: s = True ret = func(*args,**kwargs) return ret else: print(‘登录失败‘) return inner @login def shop_add(): print(‘增加一件商品‘) @login def shop_del(): print(‘删除一件商品‘) shop_add() shop_del()
原文:https://www.cnblogs.com/li33232/p/11438023.html