0

I'm playing around with lru_cache in python and struggling to get it to work >.o does anyone know what I'm doing wrong>?

from functools import lru_cache


class Foo(object):
    def __init__(self, a):
        self.a = a

    def __hash__(self) -> int:
        return hash(self.a)


@lru_cache(maxsize=None)
def test(a: Foo):
    print('hi')
    return None


if __name__ == '__main__':
    f1 = Foo(1)
    f2 = Foo(1)
    # should print 'hi'
    test(f1)
    # should NOT print 'hi', but does print 'hi'
    test(f2)
  • 1
    There was a [question on this](https://stackoverflow.com/questions/69779929/cache-object-instances-with-lru-cache-and-hash) a few days ago: if you define `__hash__` also also need to define `__eq__`, for two instances to be considered equal in the LRU cache. – sj95126 Nov 02 '21 at 22:29

0 Answers0