-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
27 additions
and
44 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
class CachedProperty(object): | ||
""" | ||
A property that is only computed once per instance and then replaces | ||
itself with an ordinary attribute. Deleting the attribute resets the | ||
property. | ||
Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 | ||
""" | ||
|
||
def __init__(self, func): | ||
self.func = func | ||
|
||
def __get__(self, obj, cls): | ||
if obj is None: | ||
return self | ||
if self.func.__name__ not in obj.__dict__: | ||
obj.__dict__[self.func.__name__] = self.func(obj) | ||
return obj.__dict__[self.func.__name__] | ||
|
||
|
||
cached_property = CachedProperty | ||
class CachedProperty: | ||
""" | ||
A property that is only computed once per instance and then replaces | ||
itself with an ordinary attribute. Deleting the attribute resets the | ||
property. | ||
Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 | ||
""" | ||
|
||
def __init__(self, func): | ||
self.func = func | ||
|
||
def __get__(self, obj, cls): | ||
if obj is None: | ||
return self | ||
if self.func.__name__ not in obj.__dict__: | ||
obj.__dict__[self.func.__name__] = self.func(obj) | ||
return obj.__dict__[self.func.__name__] | ||
|
||
|
||
cached_property = CachedProperty |