python - Object of custom type as dictionary key - Stack Overflow

PHOTO EMBED

Sat Jan 01 2022 08:41:48 GMT+0000 (Coordinated Universal Time)

Saved by @kartick_sriram #python

class MyThing:
    def __init__(self,name,location,length):
        self.name = name
        self.location = location
        self.length = length

    def __hash__(self):
        return hash((self.name, self.location))

    def __eq__(self, other):
        return (self.name, self.location) == (other.name, other.location)

    def __ne__(self, other):
        # Not strictly necessary, but to avoid having both x==y and x!=y
        # True at the same time
        return not(self == other)
content_copyCOPY

future use. to send class/objects by dict. which uses hash to store value.

https://stackoverflow.com/questions/4901815/object-of-custom-type-as-dictionary-key