Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Created October 4, 2019 12:17
Show Gist options
  • Save nathan-cruz77/f2243bc0035fe74228433e09ba75e3e7 to your computer and use it in GitHub Desktop.
Save nathan-cruz77/f2243bc0035fe74228433e09ba75e3e7 to your computer and use it in GitHub Desktop.
Simple custom object that can be used inside sets.
class Hashable:
def __init__(self, arg):
self.arg = arg
def __hash__(self):
return hash(self.arg)
def __eq__(self, other):
return self.__hash__() == other.__hash__()
class Unhashable:
def __init__(self, arg):
self.arg = arg
def __eq__(self, other):
return self.arg == other.arg
data = []
for i in [1, 1, 2, 2]:
obj = Unhashable(i)
if obj not in data:
data.append(obj)
print([obj.arg for obj in data])
data = {Hashable(i):0 for i in [1, 1, 2, 2]}
print([obj.arg for obj in data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment