返回顶部

python_装饰器

第一步
def demo(func):
    def inner():
        print ('111111')
        func()
    return inner

@demo
def home():
    print ('home')

home()
def book():
    print ('book')


def  finance():
    print('finance')
最简单的装饰器

第二步
user, passwd  = 'yang', '123456'
flag = False

def demo(func):
    def inner():
        global flag
        if flag == False:
            user_1 = input('please your name: ')
            passwd_1 = input('please your passwd: ')
            if user_1 ==  user and passwd_1 == passwd:
                flag = True
                print ('登录成功')
                func()
            else:
                print ('登录失败')
        elif flag == True:
            func()
    return inner

@demo
def home():
    print ('home')
@demo
def book():
    print ('book')
home()
book()
账号密码只认证一次

第三步
user, passwd  = 'jd', 'jd123456'    #JD账号
user1, passwd1= 'wx', 'wx123456'   #wx账号
flag = False

def args1(auth ='jd'):
    if auth =='jd':
        def demo(func):
            def inner():
                global flag
                if flag == False:
                    user_jd = input('please your name: ')
                    passwd_jd = input('please your passwd: ')
                    if user_jd ==  user and passwd_jd == passwd:
                        flag = True
                        print ('登录成功')
                        func()
                    else:
                        print ('登录失败')
                elif flag == True:
                    func()
            return inner
        return demo
    elif auth == 'wx':
        def demo(func):
            def inner():
                global flag
                if flag == False:
                    user_wx = input('please your name: ')
                    passwd_wx = input('please your passwd: ')
                    if user_wx == user1 and passwd_wx == passwd1:
                        flag = True
                        print('登录成功')
                        func()
                    else:
                        print('登录失败')
                elif flag == True:
                    func()
            return inner
        return demo


@args1()
def home():
    print('home')

@args1()
def book():
    print('book')


home()
book()
支持不同平台账号密码登录(wx or jd)

 






posted on 2018-01-11 13:58  augustyang  阅读(150)  评论(0编辑  收藏  举报

导航