-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstractcollection.py
More file actions
46 lines (40 loc) · 1.37 KB
/
abstractcollection.py
File metadata and controls
46 lines (40 loc) · 1.37 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
class AbstractCollection(object):
"""An abstract collection implementation."""
# Constructor
def __init__(self, sourceCollection=None):
"""Sets the initial state of self, which includes the
content of sourceCollection, if it's present."""
self._size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def __len__(self):
"""Returns the number of items in self."""
return self._size
def isEmpty(self):
"""Returns True if len(self) == 0, or False otherwise."""
return len(self) == 0
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
otherIter = iter(other)
for item in self:
if item != next(otherIter):
return False
return True
def __str__(self):
"""Return the string representation of self."""
return "{" + ",".join(map(self)) + "}"
# Mutator methods
def __add__(self, other):
"""Returns a new bag containing the contents
of self and other."""
result = type(self)(self)
for item in other:
result.add(item)
return result