class MyMethod(object): def __init__(self, function): self._func = function self.__name__ = function.__name__ def __call__(self, *args, **kwargs): print "Calling function", self._func res = self._func(*args, **kwargs) print "Result was", res return res def __get__(self, instance, type): """Used to get a version of the method in the context of the instance.""" if instance is None: return self def wrapper(*args, **kwargs): return self(instance, *args, **kwargs) # so the wrapper has the right function name when debugging ... wrapper.__name__ = self.__name__ return wrapper class MyClass(object): @MyMethod def foo(self, arg1, arg2): print (arg1, arg2) print "MyClass:", MyClass print "MyClass.foo", MyClass.foo c = MyClass() print "instance:", c print "instance.foo:", c.foo c.foo(1,2) print # programatically create a class FooClass = type('FooClass', (object,), {'foo': MyMethod(lambda self: 42) }) print "FooClass:", FooClass print "FooClass.foo", FooClass.foo c2 = FooClass() print "instance:", c2 print "instance.foo:", c2.foo c2.foo()