Skip to content

Instantly share code, notes, and snippets.

@Stebalien
Created December 21, 2011 05:25
Show Gist options
  • Save Stebalien/1504709 to your computer and use it in GitHub Desktop.
Save Stebalien/1504709 to your computer and use it in GitHub Desktop.
Lazily Evaluated Variable
def lazy(func):
def get(self):
val = func(self)
setattr(self, func.func_name, val)
return val
def set(self, val):
setattr(self, func.func_name, val)
return property(get, set)
@Stebalien
Copy link
Author

Decorating a method with @lazy will turn it into a property that will replace itself with its return value when evaluated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment