Skip to content

Instantly share code, notes, and snippets.

@keddad
Created May 12, 2022 15:59
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 keddad/15d546926b7355e80302c762e6df3544 to your computer and use it in GitHub Desktop.
Save keddad/15d546926b7355e80302c762e6df3544 to your computer and use it in GitHub Desktop.
class Kasper:
def __init__(self, v):
self.v = v
def __eq__(self, other):
return self.v == other.v
d = {}
d[Kasper(4)] = 5 # TypeError: unhashable type: 'Kasper'
print(Kasper(4) in d)
class Kasper:
def __init__(self, v):
self.v = v
def __hash__(self):
return hash(self.v)
def __eq__(self, other):
return self.v == other.v
d = {}
d[Kasper(4)] = 5
print(Kasper(4) in d) # True, works fine
class Kasper:
def __init__(self, v):
self.v = v
def __hash__(self):
return hash(self.v)
d = {}
d[Kasper(4)] = 5
print(Kasper(4) in d) # False, not ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment