看登录中间件的时候发现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 innerclass 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,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)@H_150_419@ 总结
以上是内存溢出为你收集整理的Django惰性加载和LazyObject全部内容,希望文章能够帮你解决Django惰性加载和LazyObject所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)