I'm wondering if it's possible to have a class which automatically wraps all of its methods, without it being necessary to explicity decorate each of them.
For example, instead of doing this:
class MyClass:
@mydecorator
def mymethod1(...):
...
@mydecorator
def mymethod2(...):
...
I'd like to do something like this:
class MyClass(metaclass=DecoratedMethods):
def mymethod1(...):
...
def mymethod2(...):
...
Here I'm hinting at metaclasses, but I'm not sure it's the right solution path.
I've just discovered the __prepare__
protocol. This would allow me to do something naive like decorate all functions or all callables in the class namespace, but that's not really what I want. I only want to decorate methods(class methods and instance methods).
Python has so many metaprogramming facilities, I'd be surprised if there wasn't a way... Or at least a better way than decorating each method manually?
I'm using python 3.6.
Thanks!