zoukankan      html  css  js  c++  java
  • 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)





  • 相关阅读:
    monaco editor
    javascript for of 和 for in 在数组和对象中的区别
    django rest 版本控制器
    学习
    day 22
    day 21
    day20
    day21 数据处理自我小结
    day20 【手写数字识别】之数据处理(搬运)
    day19 通过极简方案快速构建手写数字识别模型 (百度飞浆搬运)
  • 原文地址:https://www.cnblogs.com/augustyang/p/8268087.html
Copyright © 2011-2022 走看看