Search code examples
pythoncachinghashfunctools

functools.lru_cache difference between two object with same hash


After reading the code of https://github.com/python/cpython/blob/master/Lib/functools.py I thought that lru_cache use hash to build a key from the function arguments, so if I two object have the same hash they should be the same for lru_cache.

If you run this code:

from functools import lru_cache

COUNT=0
@lru_cache(maxsize=None)
def fnc(*args, **kvargs):
    global COUNT
    COUNT=COUNT+1
    return COUNT, hash(args[0]), args ,kvargs

class MyClass:
    def __init__(self, *args, **kvargs):
        self._init_args=(args, frozenset(kvargs.items()))
    def __hash__(self):
        return hash(self._init_args)

m1a = MyClass(1)
m2a = MyClass(2)
m1b = MyClass(1)
m2b = MyClass(2)
fnc(m1a) # Output: (1, -7270110455953140331, (<__main__.MyClass object at 0x7fb14b540710>,), {})
fnc(m1a) # Output: (1, -7270110455953140331, (<__main__.MyClass object at 0x7fb14b540710>,), {})
fnc(m2a) # Output: (2, 7567087542259278010, (<__main__.MyClass object at 0x7fb14b540810>,), {})
fnc(m2a) # Output: (2, 7567087542259278010, (<__main__.MyClass object at 0x7fb14b540810>,), {})
fnc(m1b) # Output: (3, -7270110455953140331, (<__main__.MyClass object at 0x7fb14b540610>,), {})
fnc(m2b) # Output: (4, 7567087542259278010, (<__main__.MyClass object at 0x7fb14b540b50>,), {})

you can see that lru_cache is detecting that m1a and m1b are different even they have the same hash.

What can I do in order that lru_cache doesn't differentiate between two instances of MyClass with same __init__ arguments?


Solution

  • lru_cache uses __hash__ at first then it checks __eq__
    Add __eq__ method to your class

        def __eq__(self, other):
            return type(self) == type(other) and self._init_args == other._init_args