Skip to content

Instantly share code, notes, and snippets.

@ironpythonbot
Created September 28, 2013 01:08
Show Gist options
  • Save ironpythonbot/6737299 to your computer and use it in GitHub Desktop.
Save ironpythonbot/6737299 to your computer and use it in GitHub Desktop.
CodePlex Issue #19821 Plain Text Attachments
import types
class cached_property(object):
no_value = object()
def __init__(self, fget):
self.fget = fget
self.name = '_' + fget.__name__
def __get__(self, obj, cls):
if obj is None:
return self
v = getattr(obj, self.name, cached_property.no_value)
if v is cached_property.no_value:
v = self.fget(obj)
setattr(obj, self.name, v)
return v
def reset(self, obj, v=no_value):
if v is cached_property.no_value:
v = self.fget(obj)
setattr(obj, self.name, v)
return v
class Foo(object):
@cached_property
def bar(self):
return self._attr_error();
def _attr_error(self):
return types.FunctionTyp
if __name__ == '__main__':
f = Foo()
print f.bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment