>> "Hi".__eq__("hi") >>>"> >> "Hi".__eq__("hi") >>>">
Download presentation
Presentation is loading. Please wait.
Published byInge Hermawan Modified over 5 years ago
1
Fundamentals of Programming II What Is Equality?
Computer Science 112 Fundamentals of Programming II What Is Equality?
2
What Is Equality? For collections, the == operator is syntactic sugar for a call of the __eq__ method on the left operand, with the right operand as an argument Thus, >>> "Hi" == "hi" False >>> "Hi".__eq__("hi") >>>
3
Python’s Two Equality Operators
The is operator tests two objects for identity (same address in memory) The == operator has a default implementation (__eq__ method) in the object class This implementation uses is
4
Identity vs Structural Equivalence
numbers = Array(4) # Then put 3, 2, 6, and 4 in there numbers 3 2 6 4
5
Identity vs Structural Equivalence
numbers = Array(4) # Then put 3, 2, 6, and 4 in there alias = numbers # An alias numbers and alias refer to the same object in memory numbers 3 2 6 4 alias
6
Identity vs Structural Equivalence
numbers = Array(4) # Then put 3, 2, 6, and 4 in there alias = numbers # An alias copy = resize(numbers, 1) # A real copy numbers and alias refer to the same object in memory numbers and copy refer to distinct objects whose contents are the same numbers 3 2 6 4 alias copy 3 2 6 4
7
Identity vs Structural Equivalence
>>> numbers == alias True numbers and alias refer to the same object in memory numbers and copy refer to distinct objects whose contents are the same numbers 3 2 6 4 alias copy 3 2 6 4
8
Identity vs Structural Equivalence
>>> numbers == alias True >>> numbers is alias numbers and alias refer to the same object in memory numbers and copy refer to distinct objects whose contents are the same numbers 3 2 6 4 alias copy 3 2 6 4
9
Identity vs Structural Equivalence
>>> numbers == alias True >>> numbers is alias >>> numbers is copy # Right, they are not identical False numbers and alias refer to the same object in memory numbers and copy refer to distinct objects whose contents are the same numbers 3 2 6 4 alias copy 3 2 6 4
10
Identity vs Structural Equivalence
>>> numbers == alias True >>> numbers is alias >>> numbers is copy # Right, they are not identical False >>> numbers == copy False # Counterintuitive: why not True? numbers and alias refer to the same object in memory numbers and copy refer to distinct objects whose contents are the same numbers 3 2 6 4 alias copy 3 2 6 4
11
Equality for Linear Collections
The __eq__ method is redefined in the list, tuple, and str classes This method first checks for object identity (using is) It then checks for equality of type, and then for equality of length, and finally for equality of pairs of elements at each position
12
Default Pattern for Equality Test
def __eq__(self, other) if self is other: return True if type(self) != type(other): return False if len(self) != len(other): return False otherIter = iter(other) for item in self: if item != next(otherIter) return False return True Works for linear and sorted collections, but not for unordered collections. Why not?
13
Equality for Sets Two sets are equal if they are identical
Or they are of the same type and length and contain exactly the same elements (if everything in set A is also in set B)
14
Equality Test for Sets Works for sets. Why won’t it work for bags?
def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" if self is other: return True if type(self) != type(other) or len(self) != len(other): return False for item in self: if not item in other: return True Works for sets. Why won’t it work for bags?
15
A Counterexample >>> s1 = ArraySet([1, 1, 2, 2, 2]) >>> s2 = ArraySet([1, 1, 1, 2, 2]) >>> s1 == s2 True >>> b1 = ArrayBag([1, 1, 2, 2, 2]) >>> b2 = ArrayBag([1, 1, 1, 2, 2]) >>> b1 == b2 # Should be False Two bags are equal if and only if the number of instances of each item in one is the same as the number of instances of the same item in the other
16
What is the Big-O? == for linear and sorted collections == for sets
== for bags
17
Best Case, Worst Case, Average Case: Linear and Sorted Collections
def __eq__(self, other): if self is other: return True if type(self) != type(other): return False if len(self) != len(other): return False otherIter = iter(other) for item in self: if item != next(otherIter) return False return True
18
Best Case, Worst Case, Average Case: Sets
def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" if self is other: return True if type(self) != type(other) or len(self) != len(other): return False for item in self: if not item in other: return True
19
Best Case, Worst Case, Average Case: Bags
def __eq__(self, other): """Returns True if self equals other, or False otherwise.""" if self is other: return True if type(self) != type(other) or len(self) != len(other): return False for item in self: if ???????: return True
20
How Many __eq__s Can There Be?
BagInterface LinkedBag ArrayBag ArraySortedBag Interface polymorphism: == always looks the same from the clients’ side, but __eq__ can be implemented differently to provide optimal performance
21
Inheritance and abstract classes Chapter 6
For Wednesday Inheritance and abstract classes Chapter 6
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.