zoukankan      html  css  js  c++  java
  • python 装饰器

    from time import time
    
    
    def fa(fun):
        def fb(arg):
            print('start2')
            start = time()
            result = fun(arg)
            print(time() - start)
            print('end2')
            return result
    
        return fb
    
    
    def fc(fun):
        def fb(arg):
            print('start1')
            result = fun(arg)
            print('end1')
            return result
    
        return fb
    
    
    @fc
    @fa
    def code(arg):
        print('code haha')
        return arg + '__zxl'
    
    
    # code=fa(code)
    print(code('hello'))

    输出:

    start1
    start2
    code haha
    0.0
    end2
    end1
    hello__zxl

    from time import time
    
    
    def fz(flag=True):
        def fa(fun):
            def fb(*args):
                print('打印日志') if flag else print('不打印日志')
                print('start2')
                start = time()
                result = fun(*args)
                print(time() - start)
                print('end2')
                return result
    
            return fb
    
        return fa
    
    @fz(False)
    # @fz(True)
    # @fz()
    def code(*args):
        print(args)
        return sum(args)
    
    
    print(code(3, 4, 5, 6, 7))

    输出:

    不打印日志
    start2
    (3, 4, 5, 6, 7)
    0.0
    end2
    25

    is_login = False
    
    
    def login(type='weixin'):
        def login1(fun):
            def code():
    
                global is_login
                if not is_login:
                    print(type)
                    username = input('用户名:')
                    password = input('密码:')
                    if username == 'zxl' and password == 'zxl':
                        print('登录成功')
                        is_login = True
                        fun()
                    else:
                        print('登陆失败')
                else:
                    fun()
    
            return code
    
        return login1
    
    
    @login()
    def home():
        print('主页')
    
    
    @login('QQ')
    def news():
        print('新闻')
    
    
    home()
    news()
  • 相关阅读:
    Oracle EBS json
    OAF--基础
    Oracle EBS FA 本年折旧
    Oracle EBS FA 获取累计折旧
    SOAP REST
    Oracle EBS FA 资产取值
    Java ——基础语法
    PL/SQL APIs for Concurrent Processing
    Using Globals in Oracle Reports (Doc ID 34751.1)
    Using SRW Packaged Procedures In Reports (Doc ID 61643.1)
  • 原文地址:https://www.cnblogs.com/zhaoxianglong1987/p/7563009.html
Copyright © 2011-2022 走看看