-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequal
More file actions
executable file
·32 lines (25 loc) · 729 Bytes
/
Copy pathequal
File metadata and controls
executable file
·32 lines (25 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# Demonstrate __eq__, __ne__
from dataclasses import dataclass
@dataclass(eq=False)
class Base():
x: float
class EQ(Base):
def __eq__(self, other):
print('eq')
return self.x == other.x
class NE(Base):
def __ne__(self, other):
print('ne')
return self.x != other.x
class Neither(Base): pass
f = EQ(2)
b = NE(2)
c = Neither(2)
assert f == b # calls f.__eq__(b)
assert b == f # delegates to f.__eq__(b)
assert not (f != b) # delegates to f.__eq__(b)
assert not (b != f) # calls be.__ne__(f)
assert f == c # calls f.__eq__(c)
assert c == f # delegates to f.__eq__(c)
assert False == (c == b) # no dunder method is called