好代码系列(一):LazyObject
2015-12-02 18:49
501 查看
site-packages/django/utils/functional.py
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(object): """ 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): self._wrapped = empty __getattr__ = new_method_proxy(getattr) def __setattr__(self, name, value): if name == "_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": 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,)) # We have to explicitly override __getstate__ so that older versions of # pickle don't try to pickle the __dict__ (which in the case of a # SimpleLazyObject may contain a lambda). The value will end up being # ignored by our __reduce__ and custom unpickler. def __getstate__(self): return {} 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)
相关文章推荐
- IOS-36-Object-C语法之属性关键字的使用(assign/weak/strong/copy)
- JSONObject 转换 JSON复杂对象
- Java之Object构造方法(创建子类对父类构造方法的调用)
- Object中的wait,notify,notifyAll基本使用
- Java多线程编程模式实战指南(二):Immutable Object模式
- SWFObject文件上传使用记录
- 问题3-Error occurred during initialization of VM Could not reserve enough space for object heap
- 使用Myeclipse内置Ant编译项目时提醒警告java\lang\Object.class(java\lang:Object.class): 主版本 51 比 50 新,此编译器支持最新的主版本
- 在controller中返回ajax处理后结果,页面显示xmlObjectDocument的解决方法
- Objective C语言中nil、Nil、NULL、NSNull的区别
- git 错误 fatal: Not a valid object name: 'master'.
- re.MatchObject() Python
- 基于SuperMap iObjects.NET 7C 中扩展图层实现航线一键查询系统
- #Paper Reading# Learning to Segment Object Candidates
- Qt多线程间信号槽传递非QObject类型对象的参数
- objective-c 委托的理解与应用
- Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)
- 用Spring+Hibernate做项目时候遇到 java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit
- Cloneable接口和Object的clone()方法
- Objective-C 编码建议