Skip to content

Instantly share code, notes, and snippets.

View phill-tornroth's full-sized avatar

Phill Tornroth phill-tornroth

  • http://elationhealth.com
  • Encinitas, CA
View GitHub Profile
@phill-tornroth
phill-tornroth / lazy_load.py
Created January 27, 2011 22:51
Spun up a little decorator to lazy load properties that I wanted pinned to my context object. I'm using this in the context of a web request object, where I want to make things like request.current_whatever available, but I don't want to pay the penalty f
from collections import MutableMapping
from functools import wraps
def lazy_load(f):
@property
@wraps(f)
def wrapped_fetch(self):
if f.func_name not in self._d:
self._d[f.func_name] = f(self)