Created
October 4, 2019 12:17
-
-
Save nathan-cruz77/9b2ac8d39996383d8d157403358ef0ef to your computer and use it in GitHub Desktop.
Simple custom object that can be used inside sets.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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