作业,熟悉--装饰器--格式化输出

# 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,'苹果')
View Code

 

posted @ 2019-03-14 23:45  ChenGouDa  阅读(66)  评论(0编辑  收藏  举报