You can do it with a class decorator:
def register_callback(name):
def decorator(cls):
cls._callback = getattr(cls, name)
return cls
return decorator
@register_callback('my_func')
class SimpleConsumer(Consumer):
def my_func(self, body):
print body
If you want to decorate a method, you will get only a function in it so you cannot access any information about the class that the method is contained in.
But if only one callback should be available per class why just not call it _callback
?
class SimpleConsumer(Consumer):
def _callback(self, body):
print body
Or do something like:
class SimpleConsumer(Consumer):
def my_func(self, body):
print body
_callback = my_func
?