Skip to content

Instantly share code, notes, and snippets.

@whitmo
Created October 6, 2011 18:26
Show Gist options
  • Select an option

  • Save whitmo/1268196 to your computer and use it in GitHub Desktop.

Select an option

Save whitmo/1268196 to your computer and use it in GitHub Desktop.
caching property
from functools import partial
class CachingDescriptor(object):
def __init__(self, func):
self.cache = None
self.func = func
def __get__(self, obj, objtype):
if self.cache is None:
self.cache = self.func(obj)
return self.cache
def __del__(self, obj):
self.cache = None
class Test(object):
@CachingDescriptor
def big(self):
return ["<%s>" % ('-'*x) for x in range(100)]
@whitmo
Copy link
Author

whitmo commented Oct 6, 2011

with_name is sort of silly, since the reference is contained inside the instantiated descriptor.

@whitmo
Copy link
Author

whitmo commented Oct 6, 2011

removed all name stuff as irrelevant

@whitmo
Copy link
Author

whitmo commented Oct 6, 2011

ok... this is sensible and tested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment