• python 装饰器 (test = submit(test))


    两层的装饰器,这里对于register不需要进行输入

    usename, passwd = 'alex', 'abc123'
    def register(func):
        def wrapper(*args, **kwargs):
            username = input('usename:').strip()
            passward = input('passward:').strip()
            if username == usename and passward == passwd:
                return func(*args, **kwargs)
    
        return wrapper
    
    @ register
    def index():
        print('1')
        return 'ok'
    
    print(index())

    装饰器主要使用的是 test = submit(test), 在函数中返回了submit的函数,因此需要再嵌套一个函数,用来进行返回submit的函数地址

    import time
    
    usename, passwd = 'alex', 'abc123'
    
    def submit(register_type):
        def register(func):
            def wrapper(*args, **kwargs):
                if register_type == 'login':
                    username = input('usename:').strip()
                    passward = input('passward:').strip()
                    if username == usename and passward == passwd:
                        res = func()
                        return res
                    else:
                        print('the usename is error or passward is error')
                        username = input('usename:').strip()
                        passward = input('passward:').strip()
            return wrapper
        return register
    
    @ submit(register_type='login')  # index = submit(register_type)
    def index():
        print('this is index')
        return 'yes'
    
    print(index())
  • 相关阅读:
    iOS-导航条
    iOS-存储
    iOS-模型传递
    iOS-日期相关
    iOS-UIViewController
    iOS-loadView方法
    iOS-UIWindow
    Spring 测试
    Spring条件注解@Conditional
    Spring多线程
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/11457694.html
走看看 - 开发者的网上家园