I went ahead and wrote a metaclass to solve your question. It goes through all the attributes and checks if they are callable (usually a function, method or class) and decorates those that are. Of course, you would install decorator in your memoizing decorator (e.g. functools.lru_cache ).
If you want to only decorate methods, not any called ones, you can replace the hasattr(val, "__call__") inspect.ismethod(val) with inspect.ismethod(val) . But this may lead to an error in the future when you do not remember that it only works for methods and adds a function or class that will not be stored in memory!
See this SO question for more information on metaclasses in Python.
def decorate(f): def wrap(*args, **kwargs):
jacwah
source share