-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathEqualsOrNotEquals.py
More file actions
56 lines (39 loc) · 1.28 KB
/
EqualsOrNotEquals.py
File metadata and controls
56 lines (39 loc) · 1.28 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class PointOriginal(object):
def __init__(self, x, y):
self._x, x
self._y = y
def __repr__(self):
return 'Point(%r, %r)' % (self._x, self._y)
def __eq__(self, other): # Incorrect: equality is defined but inequality is not
if not isinstance(other, Point):
return False
return self._x == other._x and self._y == other._y
class PointUpdated(object):
def __init__(self, x, y):
self._x, x
self._y = y
def __repr__(self):
return 'Point(%r, %r)' % (self._x, self._y)
def __eq__(self, other):
if not isinstance(other, Point):
return False
return self._x == other._x and self._y == other._y
def __ne__(self, other): # Improved: equality and inequality method defined (hash method still missing)
return not self == other
class A:
def __init__(self, a):
self.a = a
def __eq__(self, other):
print("A eq")
return self.a == other.a
def __ne__(self, other):
print("A ne")
return self.a != other.a
class B(A):
def __init__(self, a, b):
self.a = a
self.b = b
def __eq__(self, other):
print("B eq")
return self.a == other.a and self.b == other.b
print(B(1,2) != B(1,3))