flask之request.method语句执行流程

执行流程:

全局属性request对象:
    request = LocalProxy(partial(_lookup_req_object, "request"))
    # request添加属性:
    # self._LocalProxy__local=partial(_lookup_req_object, "request")
    # self.__wrapped__=partial(_lookup_req_object, "request")        
        
1. request.method执行流程:    
    request = LocalProxy(partial(_lookup_req_object, "request"))
    request.method = LocalProxy(partial(_lookup_req_object, "request")).method
    
2. 执行request对象__getattr__方法, 最终得到:partial(_lookup_req_object, "request").method
    def __getattr__(self, name):  # name=method
        if name == "__members__":
            return dir(self._get_current_object())
            
        # getattr(self._get_current_object(), 'method'), self=LocalProxy对象
            # 即self._get_current_object() = partial(_lookup_req_object, "request")
            # 故:getattr(self._get_current_object(), 'method') = partial(_lookup_req_object, "request").method
        return getattr(self._get_current_object(), name)    


    def _get_current_object(self):
        """Return the current object.  This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        """
        if not hasattr(self.__local, "__release_local__"):
            # self.__local =  partial(_lookup_req_object, "request")
            return self.__local()  
        try:
            return getattr(self.__local, self.__name__) # 在初始化的时候:object.__setattr__(self, "_LocalProxy__local", local)
        except AttributeError:
            raise RuntimeError("no object bound to %s" % self.__name__)
            
3. 最终执行partial(_lookup_req_object, "request").method        
    partial(_lookup_req_object, "request").method = partial(_lookup_req_object, "request").method = _lookup_req_object("request").method
    本质上是执行:_lookup_req_object("request").method    
        _lookup_req_object("request") 返回上下文request对象(线程安全), 该request对象包含method属性
        def _lookup_req_object(name): # name="request"
        # 获取上下文self._local.stack[-1]--(线程id/协程内存地址标识)
        top = _request_ctx_stack.top
        if top is None:
            raise RuntimeError(_request_ctx_err_msg)
        # 等价于:self._local.stack[-1].request获取到request对象
        return getattr(top, name)    

私有属性的定义与获取:

class A(object):
    def __init__(self):
        # 类内部定义私有属性的三种方式
        object.__setattr__(self, '_A__gender', 'male')
        self._A__name = 'wyq'
        self.__age = 28

    def __call__(self, *args, **kwargs):
        # 类内部获取私有属性
        print(self.__name)
        print(self._A__name)
        print(self.__age)
        print(self._A__age)
        print(self.__gender)
        print(self._A__gender)


a = A()
a()
# 类外部获取私有属性
print(a._A__name)
print(a._A__age)
print(a._A__gender)

转载于:https://www.cnblogs.com/wuyongquan/p/11375637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值