flask의 Request.method 문 실행 프로세스

8958 단어

실행 프로세스:

 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

좋은 웹페이지 즐겨찾기