Created
May 15, 2019 23:23
-
-
Save ezheidtmann/72fc8d621570d638c441a94eb468e916 to your computer and use it in GitHub Desktop.
Cached attribute pattern example
This file contains hidden or 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
# A basic cached attribute pattern | |
def compute_a_value(): | |
return 123 + 45 | |
class A: | |
@property | |
def a(self): | |
if not hasattr(self, '_a'): | |
self._a = compute_a_value() | |
return self._a | |
assert A().a == 168 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment