Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Created October 4, 2019 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathan-cruz77/9b2ac8d39996383d8d157403358ef0ef to your computer and use it in GitHub Desktop.
Save nathan-cruz77/9b2ac8d39996383d8d157403358ef0ef 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