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
>>> p = Point(1, 2) # with both __hash__ and __eq__ | |
>>> d = {p: 10} | |
>>> p.x = 3 | |
>>> print(d) | |
{Point(3, 2): 10} | |
>>> Point(3, 2) in d | |
False |
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 Point: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
def __hash__(self): | |
return hash((self.x, self.y)) | |
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 Point: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
def __eq__(self, other): | |
return self.x == other.x and self.y == other.y | |
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 Point: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
def __eq__(self, other): | |
return self.x == other.x and self.y == other.y | |
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 Point: | |
def __init__(self, x, y): | |
self.x, self.y = x, y | |
def __repr__(self): | |
return f'Point({self.x}, {self.y})' | |
points = [Point(1,2), Point(2,3), Point(1,2)] |
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 A(tuple): | |
def __new__(cls, u, v, comment): | |
obj = super().__new__(cls, (u, v)) | |
obj.comment = comment | |
return obj | |
>>> A(1, 2, 'first') == A(1, 2, 'second') | |
True | |
>>> A(3, 4, 'first') < A(3, 5, 'second') |
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 A(list): | |
def __init__(self, u, v, comment): | |
self.comment = comment # comment is ignored in comparisons | |
super().__init__((u, v)) # u, v are used in comparisons | |
>>> A(1, 2, 'first') == A(1, 2, 'second') | |
True | |
>>> A(3, 4, 'first') < A(3, 5, 'second') | |
True |
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 namedtuple | |
A = namedtuple('A', ['u', 'v']) | |
>>> A(1, 2) == A(1, 2) | |
True | |
>>> A(3, 5) < A(3, 6) | |
True |
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
@total_ordering | |
class A: | |
def __init__(self, v): | |
self.v = v | |
def __lt__(self, other): | |
print(f'A: {self.v} < {other.v} ?') | |
return self.v < other.v | |
class B(A): |
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 A: | |
def __init__(self, v): | |
self.v = v | |
def __lt__(self, other): | |
print(f'A: {self.v} < {other.v} ?') | |
return self.v < other.v | |
class B(A): | |
def __lt__(self, other): |