zoukankan      html  css  js  c++  java
  • 装饰器执行顺序

    堆叠装饰器执行顺序

    def create_response(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            print(2)
            print(*args, **kwargs)
            resp = func(self, *args, **kwargs)
            print(3)
            # convert response of data service to the skynet format
            # status_code = resp.status_code
            if hasattr(resp, "status_code") and resp.status_code in (200, 201):
                content = resp.content
                return ParseResponse(content).response()
            else:
                return MyResponse(
                    status_code=-1, data=resp, message=resp).fail()
        return wrapper
    
    
    def process_log_id(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            print(1)
            headers = request.headers
            log_id = headers.get(HEADER_LOG_ID)
            if log_id:
                setattr(self, "log_id", log_id)
            resp = func(self, *args, **kwargs)
            print(4)
            if hasattr(self, "log_id"):
                log_id = getattr(self, "log_id")
                resp.headers[HEADER_LOG_ID] = log_id
            return resp
        return wrapper


    @process_log_id
    def test():
    print("test")

    >>> 1 2 3 4
    和django中间件执行顺序类似
  • 相关阅读:
    货币
    沙漏
    秋季学习总结
    三个老师
    介绍自己
    redis 的部分配置
    第二次博客作业
    第一次阅读作业
    shell_通配符
    shell_/dev/null,>和&
  • 原文地址:https://www.cnblogs.com/buxizhizhoum/p/14277613.html
Copyright © 2011-2022 走看看