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)





  • 相关阅读:
    强关联二维材料1T—TaS2晶体
    超薄二维Mo2C晶体
    稀有的二维狄拉克材料
    大自然中的几何植物
    字符串的内建函数
    python数据模型(特殊方法)
    插入排序
    下载谷歌浏览器(Chrome)扩展离线安装包crx文件最简单的方法
    33. 高精度练习之乘法
    32. 整数加法
  • 原文地址:https://www.cnblogs.com/augustyang/p/8268087.html
Copyright © 2011-2022 走看看