Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming II Equality and Multiple Inheritance

Similar presentations


Presentation on theme: "Fundamentals of Programming II Equality and Multiple Inheritance"— Presentation transcript:

1 Fundamentals of Programming II Equality and Multiple Inheritance
Computer Science 112 Fundamentals of Programming II Equality and Multiple Inheritance

2 Exploiting Inheritance
I want to be a complete freeloader Where can I go to get the most stuff for free? The stuff includes methods and data

3 Algorithms: Three Considerations
Correctness – will this algorithm work for all cases? Performance – is this the fastest algorithm possible? Reusability – can more than one type of object use this algorithm?

4 Default Pattern for Equality Tests
def __eq__(self, other) """Returns True if self equals other, or False otherwise.""" 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 What is the runtime performance? Why won’t this work for unordered collections?

5 Equality Test for Sets Why won’t this work for linear collections?
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 Why won’t this work for linear collections? Why won’t this work for bags? What about sorted sets?

6 Equality Test for Bags Will this work for 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 self.count(item) != other.count(item): return True Will this work for sets? If so, will it perform as well? What about sorted bags?

7 Reuse by Inheritance Avoid reinvention of the wheel!
class AbstractBag(AbstractCollection): """Common methods 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 self.count(item) != other.count(item): return True Avoid reinvention of the wheel!

8 Reuse by Inheritance Avoid reinvention of the wheel!
class AbstractSet(object): """Common methods for 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 Avoid reinvention of the wheel!

9 Reuse by Calling Up Avoid reinvention of the wheel!
from arraybag import ArrayBag from abstractcollection import AbstractCollection class ArraySortedBag(ArrayBag): """Array-based implementation of a sorted set.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" ArrayBag.__init__(self, sourceCollection) # Accessor method def __eq__(self, item): """Returns True if self equals other, or False otherwise.""" return AbstractionCollection.__eq__(self, other) Avoid reinvention of the wheel!

10 Simple Inheritance AbstractBag LinkedBag ArrayBag ArraySortedBag

11 Multiple Inheritance AbstractSet AbstractBag LinkedBag ArrayBag
LinkedSet ArraySortedBag

12 Mutiple Inheritance from linkedbag import LinkedBag from abstractset import AbstractSet class LinkedSet(AbstractSet, LinkedBag): """Linked implementation of a set.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" LinkedBag.__init__(self, sourceCollection) # Mutator method def add(self, item): """Adds item to self.""" if not item in self: LinkedBag.add(self, item) AbstractBag and AbstractSet implement different __eq__s, so list AbstractSet first to get its __eq__

13 Multiple Inheritance AbstractSet AbstractBag LinkedBag ArrayBag
LinkedSet ArraySortedBag ArraySortedSet

14 Mutiple Inheritance from arraysortedbag import ArraySortedBag from abstractset import AbstractSet class ArraySortedSet(ArraySortedBag, AbstractSet): """Array-based implementation of a sorted set.""" # Constructor def __init__(self, sourceCollection = None): """Sets the initial state of self, which includes the contents of sourceCollection, if it's present.""" ArraySortedBag.__init__(self, sourceCollection) # Mutator method def add(self, item): """Adds item to self.""" if not item in self: ArraySortedBag.add(self, item) ArraySortedBag is now listed first, to get its __eq__


Download ppt "Fundamentals of Programming II Equality and Multiple Inheritance"

Similar presentations


Ads by Google