Created
September 28, 2013 01:08
-
-
Save ironpythonbot/6737299 to your computer and use it in GitHub Desktop.
CodePlex Issue #19821 Plain Text Attachments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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