Created
June 30, 2013 15:07
-
-
Save sloria/5895501 to your computer and use it in GitHub Desktop.
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
# Even better | |
def lazy_property(fn): | |
'''Decorator that makes a property lazy-evaluated. | |
''' | |
attr_name = '_lazy_' + fn.__name__ | |
@property | |
def _lazy_property(self): | |
if not hasattr(self, attr_name): | |
setattr(self, attr_name, fn(self)) | |
return getattr(self, attr_name) | |
return _lazy_property | |
class Person: | |
def __init__(self, name, occupation): | |
self.name = name | |
self.occupation = occupation | |
@lazy_property | |
def relatives(self): | |
# Get all relatives | |
relatives = ... | |
return relatives | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
License please?