def instance_decorator(decorator):
 class Decorator(object, ):
  def __init__(self, func=nop, *args, **kws):
   self.__name__ = func.__name__
   self.__doc__ = func.__doc__
   self._data_name = ('%s_%d_decorated_instance' % (func.__name__, id(self)))
   self._func = func
   self._args = args
   self._kws = kws
  def __get__(self, obj, cls=None):
   if (obj is None):
    return
   data_name = self._data_name
   try:
    return obj.__dict__[data_name]
   except KeyError:
    decorated = decorator(obj, self._func, *self._args, **self._kws)
    obj.__dict__[data_name] = decorated
    return decorated
 return Decorator

def instance_decorator ( decorator ) : class Decorator ( object , ) : def __init__ ( self , func = nop , * args , ** kws ) : self . __name__ = func . __name__ self . __doc__ = func . __doc__ self . _data_name = ( '%s_%d_decorated_instance' % ( func . __name__ , id ( self ) ) ) self . _func = func self . _args = args self . _kws = kws def __get__ ( self , obj , cls = None ) : if ( obj is None ) : return data_name = self . _data_name try : return obj . __dict__ [ data_name ] except KeyError : decorated = decorator ( obj , self . _func , * self . _args , ** self . _kws ) obj . __dict__ [ data_name ] = decorated return decorated return Decorator
meta-decorator to define decorators that decorate a method in a concrete instance .