Created
May 12, 2022 15:59
-
-
Save keddad/15d546926b7355e80302c762e6df3544 to your computer and use it in GitHub Desktop.
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 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) |
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 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 |
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 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