1 """Borrowed from Django."""
3 from threading import Lock
5 class LazyObject(object):
7 A wrapper for another class that can be used to delay instantiation of the
10 By subclassing, you have the opportunity to intercept and alter the
11 instantiation. If you don't need to do that, use SimpleLazyObject.
17 def __getattr__(self, name):
19 if self._wrapped is None:
22 return getattr(self._wrapped, name)
24 def __setattr__(self, name, value):
25 if name in ["_wrapped", "_lock"]:
26 # Assign to __dict__ to avoid infinite __setattr__ loops.
27 self.__dict__[name] = value
29 if self._wrapped is None:
31 setattr(self._wrapped, name, value)
33 def __delattr__(self, name):
34 if name == "_wrapped":
35 raise TypeError("can't delete _wrapped.")
36 if self._wrapped is None:
38 delattr(self._wrapped, name)
42 Must be implemented by subclasses to initialise the wrapped object.
44 raise NotImplementedError
46 # introspection support:
47 __members__ = property(lambda self: self.__dir__())
50 if self._wrapped is None:
52 return dir(self._wrapped)