Skip to content

Instantly share code, notes, and snippets.

@ezheidtmann
Created May 15, 2019 23:23
Show Gist options
  • Save ezheidtmann/72fc8d621570d638c441a94eb468e916 to your computer and use it in GitHub Desktop.
Save ezheidtmann/72fc8d621570d638c441a94eb468e916 to your computer and use it in GitHub Desktop.
Cached attribute pattern example
# 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