Last active
October 30, 2019 09:40
-
-
Save fabiosoft/2a49cc97aa208c5f7aba54b69d18c36f to your computer and use it in GitHub Desktop.
Elegant ways to support equivalence ("equality") in Python classes
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
#source: https://stackoverflow.com/questions/390250/elegant-ways-to-support-equivalence-equality-in-python-classes | |
class Number: | |
def __init__(self, number): | |
self.number = number | |
def __eq__(self, other): | |
"""Overrides the default implementation""" | |
if isinstance(other, Number): | |
return self.number == other.number | |
return NotImplemented | |
def __ne__(self, other): | |
"""Overrides the default implementation (unnecessary in Python 3)""" | |
x = self.__eq__(other) | |
if x is not NotImplemented: | |
return not x | |
return NotImplemented | |
def __hash__(self): | |
"""Overrides the default implementation""" | |
return hash(tuple(sorted(self.__dict__.items()))) | |
class SubNumber(Number): | |
pass | |
n1 = Number(1) | |
n2 = Number(1) | |
n3 = SubNumber(1) | |
n4 = SubNumber(4) | |
assert n1 == n2 | |
assert n2 == n1 | |
assert not n1 != n2 | |
assert not n2 != n1 | |
assert n1 == n3 | |
assert n3 == n1 | |
assert not n1 != n3 | |
assert not n3 != n1 | |
assert not n1 == n4 | |
assert not n4 == n1 | |
assert n1 != n4 | |
assert n4 != n1 | |
assert len(set([n1, n2, n3, ])) == 1 | |
assert len(set([n1, n2, n3, n4])) == 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment