Last active
October 6, 2016 12:47
-
-
Save RoelandMatthijssens/e29d10e9325edb920d7ae676a3e855d4 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
from collections import Counter | |
class Score(): | |
def __init__(self, name, points): | |
self.name = name | |
self.points = points | |
def __hash__(self): | |
return hash(tuple([self.name, self.points])) | |
def __eq__(self, other): | |
return self.name == other.name and self.points == other.points | |
def __ne__(self, other): | |
return self.name != other.name or self.points != other.points | |
x = Counter([ | |
Score('John', 1), | |
Score('Jeff', 2), | |
Score('Bob', 3), | |
Score('Dave', 4), | |
]) | |
y = Counter([ | |
Score('John', 1), | |
Score('Bob', 3), | |
Score('Jeff', 2), | |
Score('Dave', 4), | |
]) | |
assert x == y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tip for your
__eq__
implementation: you can usereturn self.__dict__ == other.__dict__
as http://stackoverflow.com/questions/1227121/compare-object-instances-for-equality-by-their-attributes-in-python suggests. Also: do you need to override__ne__
if you implement__eq__
?