I use custom objects as keys in a python dictionary. These objects have a default hash and eq methods that are used in the default comparison. But in some function I need to use a different way to compare these objects. So, is there a way to override or pass a new mapper to compare these keys only for this particular function.
Updated: my class has the following type of functionality (I canβt edit the hash method here, it will affect a lot in other places)
class test(object): def __init__(self,name,city): self.name=name self.city=city def __eq__(self,other): hash_equality= (self.name==other.name) if(not hash_equality):
This is normal functionality. But in one specific method, I want to override / or pass in a new custom resolver where the new hash code looks like
def __hash__(self): return self.name.lower().__hash__()
so c in my_dict
returns ture
or my_dict[c] will return "obj1"
Sorry for so many updates.
As with sorting, we can pass the custom method as a comparator, is there a way to do the same here.
source share