Skip to content

Instantly share code, notes, and snippets.

@zenwerk
Created December 11, 2013 16:38
Show Gist options
  • Save zenwerk/7913823 to your computer and use it in GitHub Desktop.
Save zenwerk/7913823 to your computer and use it in GitHub Desktop.
Python Class Decorator
class test(object):
"""A property whose value is computed only once. """
def __init__(self, function):
self._function = function
def __get__(self, obj, _=None):
if obj is None:
return self
value = self._function(obj)
setattr(obj, self._function.func_name, value)
return value
def __call__(self):
print self._function
print "this is test class"
@test
def hoge():pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment