zoukankan      html  css  js  c++  java
  • Django惰性加载和LazyObject

    看登录中间件的时候发现request.user返回的是SimpleOject对象,往下看翻到了LazyObject,看源码看了半天没看懂

    网上搜了一堆资料了解下惰性加载实现是的什么功能,再回去看源码,大概知道了LazyObject实现的原理

    Django的惰性加载,就是生成对象的时候先不实例化,等到需要调用对象的属性或方法的时候再做实例化的操作

    LazyObject源码:

    empty = object()
    
    
    #为类方法实现通用的惰性加载方法,即查看对象是否实例化
    #若还未实例化,调用子类实例化方法
    #最后在生成的实例上调用原来的func类方法
    def new_method_proxy(func):
        def inner(self, *args):
            if self._wrapped is empty:
                self._setup()
            return func(self._wrapped, *args)
        return inner
    
    
    class LazyObject:
        """
        A wrapper for another class that can be used to delay instantiation of the
        wrapped class.
    
        By subclassing, you have the opportunity to intercept and alter the
        instantiation. If you don't need to do that, use SimpleLazyObject.
        """
    
        # Avoid infinite recursion when tracing __init__ (#19456).
        _wrapped = None
    
        def __init__(self):
            # Note: if a subclass overrides __init__(), it will likely need to
            # override __copy__() and __deepcopy__() as well.
            self._wrapped = empty  #标记是否有实例化
    
        __getattr__ = new_method_proxy(getattr)  #调用通用的延迟实例化方法
    
        def __setattr__(self, name, value):
            if name == "_wrapped": #若修改的为_wrapped,特殊处理
                # Assign to __dict__ to avoid infinite __setattr__ loops.
                self.__dict__["_wrapped"] = value
            else:
                if self._wrapped is empty:
                    self._setup()
                setattr(self._wrapped, name, value)   #实例化后,修改实例的属性
    
        def __delattr__(self, name):
            if name == "_wrapped":  #不可删除_wrapped属性
                raise TypeError("can't delete _wrapped.")
            if self._wrapped is empty:
                self._setup()
            delattr(self._wrapped, name)
    
        def _setup(self):
            """
            Must be implemented by subclasses to initialize the wrapped object.
            """
            #只可由子类调用
            raise NotImplementedError('subclasses of LazyObject must provide a _setup() method')
    
        # Because we have messed with __class__ below, we confuse pickle as to what
        # class we are pickling. We're going to have to initialize the wrapped
        # object to successfully pickle it, so we might as well just pickle the
        # wrapped object since they're supposed to act the same way.
        #
        # Unfortunately, if we try to simply act like the wrapped object, the ruse
        # will break down when pickle gets our id(). Thus we end up with pickle
        # thinking, in effect, that we are a distinct object from the wrapped
        # object, but with the same __dict__. This can cause problems (see #25389).
        #
        # So instead, we define our own __reduce__ method and custom unpickler. We
        # pickle the wrapped object as the unpickler's argument, so that pickle
        # will pickle it normally, and then the unpickler simply returns its
        # argument.
        def __reduce__(self):
            if self._wrapped is empty:
                self._setup()
            return (unpickle_lazyobject, (self._wrapped,))
    
        def __copy__(self):
            if self._wrapped is empty:
                # If uninitialized, copy the wrapper. Use type(self), not
                # self.__class__, because the latter is proxied.
                return type(self)()
            else:
                # If initialized, return a copy of the wrapped object.
                return copy.copy(self._wrapped)
    
        def __deepcopy__(self, memo):
            if self._wrapped is empty:
                # We have to use type(self), not self.__class__, because the
                # latter is proxied.
                result = type(self)()
                memo[id(self)] = result
                return result
            return copy.deepcopy(self._wrapped, memo)
    
        __bytes__ = new_method_proxy(bytes)
        __str__ = new_method_proxy(str)
        __bool__ = new_method_proxy(bool)
    
        # Introspection support
        __dir__ = new_method_proxy(dir)
    
        # Need to pretend to be the wrapped class, for the sake of objects that
        # care about this (especially in equality tests)
        __class__ = property(new_method_proxy(operator.attrgetter("__class__")))
        __eq__ = new_method_proxy(operator.eq)
        __lt__ = new_method_proxy(operator.lt)
        __gt__ = new_method_proxy(operator.gt)
        __ne__ = new_method_proxy(operator.ne)
        __hash__ = new_method_proxy(hash)
    
        # List/Tuple/Dictionary methods support
        __getitem__ = new_method_proxy(operator.getitem)
        __setitem__ = new_method_proxy(operator.setitem)
        __delitem__ = new_method_proxy(operator.delitem)
        __iter__ = new_method_proxy(iter)
        __len__ = new_method_proxy(len)
        __contains__ = new_method_proxy(operator.contains)
  • 相关阅读:
    nextSibling VS nextElementSibling
    线程实现连续启动停,并在某一时间段内运行
    线程:安全终止与重启
    监控知识体系
    后台服务变慢解决方案
    Java泛型类型擦除以及类型擦除带来的问题
    常见的 CSRF、XSS、sql注入、DDOS流量攻击
    Spring对象类型——单例和多例
    一次线上OOM过程的排查
    深入浅出理解基于 Kafka 和 ZooKeeper 的分布式消息队列
  • 原文地址:https://www.cnblogs.com/EmptyRabbit/p/11298603.html
Copyright © 2011-2022 走看看