Skip to content

Instantly share code, notes, and snippets.

@Conaclos
Last active December 14, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Conaclos/5068068 to your computer and use it in GitHub Desktop.
Save Conaclos/5068068 to your computer and use it in GitHub Desktop.
def stable (value):
return lambda *args, **kwargs: value
def once_per_object (unstable):
"""Method decorator. Run only once 'unstable' per instance"""
def stabilize (*args, **kwargs):
self = args [0]
result = unstable (*args, **kwargs)
setattr (self, unstable.__name__, stable (result))
return result
return stabilize
def once (unstable):
"""Method decorator. Run only once 'unstable'."""
def stabilize (*args, **kwargs):
self = args [0]
result = unstable (*args, **kwargs)
setattr (self.__class__, unstable.__name__, stable (result))
return result
return stabilize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment