装饰器

没有修改被修饰函数的源代码,也没有修改被修饰函数的调用方式,但是也没有为被修饰函数添加新功能
# 带参数验证登录的装饰器
user_lisr = [{"user_name": "aitg", "passwd": "123456"}, {"user_name": "aitg1", "passwd": "123456"},
             {"user_name": "yuyue", "passwd": "123456"}, {"user_name": "yuyue1", "passwd": "123456"}]

yonghuxinxi = {"user_name": "None", "passwd": None}


def yz(func):
    def gn(*args,**kwargs):
        if yonghuxinxi["user_name"] != "None" and yonghuxinxi["passwd"] == True:
            print('%s你好,欢迎回来到京东' % yonghuxinxi["user_name"])
            kes = func(*args,**kwargs)
            return kes
        else:
            name = input('请输入账号').strip()
            pass_wd = input('请输入密码').strip()
            for i in user_lisr:
                if name == i['user_name'] and pass_wd == i['passwd']:
                    yonghuxinxi["user_name"] = name
                    yonghuxinxi["passwd"] = True
                    print('%s你好,欢迎回来到京东' % (name))
                    kes = func(*args,**kwargs)
                    return kes
            else:
                print("账号或密码错误请重新登陆")

    return gn


def index(*args,**kwargs):
    print('欢迎来到京东')


@yz
def home(*args,**kwargs):
    print('欢迎来到个人中心' )
    print(*args)


@yz
def shopping_car(*args,**kwargs):
    print('购物车')


@yz
def order(*args,**kwargs):
    print('订单中心')

 

 
posted @ 2018-04-07 21:25  Hi小白  阅读(135)  评论(0编辑  收藏  举报