Thread safe lazy property caching for python.

class Math:
@property
def tao(self):
pass
# way you use it is
math = Math()
# access the property
tao = math.tao
class App:  @property
def name(self):
# some computation
_name = getattr(sys.modules['__main__'], '__file__', None)
if _name is None:
return '__main__'
return custom_name
app = App()# this will call the method and assign the result to `app_id` 
app_id = app.name
from threading import RLock# senitel
_missing = object()
class cached_property(object): def __init__(self, func):
self.__name__ = func.__name__
self.__module__ = func.__module__
self.__doc__ = func.__doc__
self.func = func
self.lock = RLock()
def __get__(self, obj, type=None):
if obj is None:
return self
with self.lock:
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value
class App:  @cached_property
def logger(self):
return create_logger(self)
@cached_property
def name(self):
_name = getattr(sys.modules['__main__'], '__file__', None)
if _name is None:
return '__main__'
return custom_name
app = App() 
# runs app.name for the first time and caches
app.name
# uses the cached value
app.name + '_PRODUCTION'
  1. Avoid eager object instantiation in constructor/__init__ and offload object creation only when it is needed. For example: creating logging instance only when program starts to log, connecting to database only when program needs to persist data.
  2. Use cached property if you don’t require to re-run the computation every time you need to access the property
  3. Use cached_property if you need to lazily load/create objects during runtime only when it is accessed so that your initial startup time can be minimized.

--

--

https://github.com/RobusGauli

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store