![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), # 要求登录成功一次,后续的函数都无需再输入用户名和密码\ bool_ = False def login(func): def inner(*args,**kwargs): global bool_ '''登陆''' if bool_: ret = func(*args, **kwargs) return ret else: username = input('帐号>>>') password = input('密码>>>') if username == 'chen' and password == 'jun': bool_ = True ret = func(*args, **kwargs) return ret else: print('登陆失败') return inner @login def shoplist_add(*args): print('增加{}件{}物品'.format(*args)) @login def shoplist_del(*args): print('删除%s件%s物品'%args) shoplist_add(5,'香蕉') shoplist_del(2,'苹果')