好代码系列(一):LazyObject

site-packages/django/utils/functional.py
 1 def new_method_proxy(func):
 2     def inner(self, *args):
 3         if self_wrapped is empty:
 4             self._setup()
 5         return func(self._wrapped, *args)
 6     return inner
 7 
 8 class LazyObject(object):
 9     """
10     A wrapper for another class that can be used to delay instantiation of the wrapped class.
11 
12     By subclassing, you have the opportunity to intercept and alter the
13 instantiation. If you don't need to do that, use SimpleLazyObject.
14     """
15 
16     # Avoid infinite recursion when tracing __init__ (#19456).
17     _wrapped = None
18 
19     def __init__(self):
20         self._wrapped = empty
21 
22     __getattr__ = new_method_proxy(getattr)
23 
24     def __setattr__(self, name, value):
25         if name == "_wrapped":
26             # Assign to __dict__ to avoid infinite __setattr__ loops.
27             self.__dict__["_wrapped"] = value
28         else:
29             if self._wrapped is empty:
30                 self._setup()
31             setattr(self._wrapped, name, value)
32 
33     def __delattr__(self, name):
34         if name == "_wrapped":
35             raise TypeError("can't delete _wrapped.")
36         if self._wrapped is empty:
37             self._setup()
38         delattr(self._wrapped, name)
39 
40     def _setup(self):
41         """
42         Must be implemented by subclasses to initialize the wrapped object.
43         """
44         raise NotImplementedError('subclasses of LazyObject must provide a _setup() method')
45 
46     # Because we have messed with __class__ below, we confuse pickle as to what
47     # class we are pickling. We're going to have to initialize the wrapped
48     # object to successfully pickle it, so we might as well just pickle the
49     # wrapped object since they're supposed to act the same way.
50     #
51     # Unfortunately, if we try to simply act like the wrapped object, the ruse
52     # will break down when pickle gets our id(). Thus we end up with pickle
53     # thinking, in effect, that we are a distinct object from the wrapped
54     # object, but with the same __dict__. This can cause problems (see #25389).
55     #
56     # So instead, we define our own __reduce__ method and custom unpickler. We
57     # pickle the wrapped object as the unpickler's argument, so that pickle
58     # will pickle it normally, and then the unpickler simply returns its
59     # argument.
60     def __reduce__(self):
61         if self._wrapped is empty:
62             self._setup()
63         return (unpickle_lazyobject, (self._wrapped,))
64 
65     # We have to explicitly override __getstate__ so that older versions of
66     # pickle don't try to pickle the __dict__ (which in the case of a
67     # SimpleLazyObject may contain a lambda). The value will end up being
68     # ignored by our __reduce__ and custom unpickler.
69     def __getstate__(self):
70         return {}
71 
72     def __deepcopy__(self, memo):
73         if self._wrapped is empty:
74             # We have to use type(self), not self.__class__, because the
75             # latter is proxied.
76             result = type(self)()
77             memo[id(self)] = result
78             return result
79         return copy.deepcopy(self._wrapped, memo)

 

posted @ 2015-12-02 18:49  高山流的不是水  阅读(446)  评论(0编辑  收藏  举报