Last active
December 14, 2015 09:49
-
-
Save Conaclos/5068068 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
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