Python memoization of class instance methods, can/should we use functools cache?

Viewed 238

My understanding is that functools @cache decorator will memoize on the arguments. However for instance methods of a class, the arguments include 'self'. Would that have any performance impact?

class Test:
    def __init__(self):
        self.cache = {}

    def test(self, a, b):
        result = self.cache.get((a,b))
        if result is None:
            result = a + b
            self.cache[(a,b)] = result
        return result

Would the above perform worse than simply decorating test() with @functools.cache?

0 Answers
Related