• Python 基础 -2.4.1 装饰器


    不带参数的装饰器

    # 不带参数的装饰器
    login_status = False
    def login(fun):
        def inner(*args,**kwargs):
            _name = "cmz"
            _passwd = "cmz"
            global login_status
            if not login_status:
                name = input("name>").strip()
                pwd = input("pwd>").strip()
                if name == _name and pwd == _passwd:
                    print("welcome")
                    login_status = True
            if login_status:
                fun(*args,**kwargs)
        return inner
    
    @login
    def fun1():
        print("f1")
    @login
    def fun2():
        print("f2")
    @login
    def fun3():
        print("f3")
    fun1()
    # fun2()
    fun3()

    带参数的装饰器

    log_status = False
    def login(auth_type):
        def auth(fun):
            def inner(*args,**kwargs):
                if auth_type == "qq":
                    _name = "cmz"
                    _pwd = "cmz"
                    global log_status
                    if not log_status:
                        name = input("name> ").strip()
                        passwd = input("passwd > ").strip()
                        if name == _name and passwd == _pwd:
                            print("welcome..")
                            log_status = True
                    if log_status:
                        fun(*args,**kwargs)
                else:
                    print("只支持qq")
            return inner
        return auth
    
    
    @login("qq")
    def fun1():
        print("f1")
    
    @login("ac")
    def fun2():
        print("f2")
    
    
    fun1()
    fun2()
  • 相关阅读:
    ios录音
    ios 音乐播放
    ios程序播放音频文件
    ios摇一摇截屏代码
    加速计
    蓝牙4.0实现及原理
    物理仿真
    iOS 监听耳机变化
    删除缓存
    宏定义加参数
  • 原文地址:https://www.cnblogs.com/caimengzhi/p/8324161.html
Copyright © 2020-2023  润新知