Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

2.3 Implementing a Container

We begin the implementation of the Bag ADT with the constructor. The constructor defines a single data field, which is initialized to an empty list. This corresponds to the definition of the constructor for the Bag ADT in which the container is initially created empty.

class Bag :
  def __init__( self ):
    self._theItems = list()

A sample instance of the Bag class created from the example checkdates2.py program provided earlier is illustrated in Figure 2.3.1.

Figure 2.3.1: Sample instance of the Bag class implemented using a list.

Most of the implementation details for the remaining methods follow the specifics discussed in the previous section.

def __len__(self) :
  return len(self._theItems)

def __contains__(self, item) :
  return item in self._theItems

def add(self, item) :
  self._theItems.append(item)

There are some additional details, however. First, the ADT definition of the remove operation specifies the precondition that the item must exist in the bag in order to be removed. Thus, we must first assert that condition and verify the existence of the item.

def remove(self, item) :
  assert item in self._theItems, "The item must be in the bag."
  ndx = self._theItems.index(item)
  return self._theItems.pop(ndx)

Second, we need to provide an iteration mechanism that allows us to iterate over the individual items in the bag. We delay the implementation of this operation until the next section where we discuss the creation and use of iterators in Python.

The complete implementation of the Bag ADT using a list is provided in the program listing below. Note that instances of the Bag class are mutable objects. That is, the contents of the object can be changed. The objects created from all of the classes presented in the previous chapter were immutable objects. See the Special Topic at the bottom of the page for a refresher on the difference between mutable and immutable objects.

Program Listing
Program: listbag.py
  1. # Implements the Bag ADT container using a Python list.
  2. class Bag :
  3.    # Constructs an empty bag.
  4.   def __init__(self) :
  5.     self._theItems = list()
  6.  
  7.    # Returns the number of items in the bag.
  8.   def __len__(self) :
  9.     return len(self._theItems)
  10.  
  11.    # Determines if an item is contained in the bag.
  12.   def __contains__(self, item) :
  13.     return item in self._theItems
  14.    
  15.    # Adds a new item to the bag.
  16.   def add(self, item) :
  17.     self._theItems.append(item)
  18.    
  19.    # Removes and returns an instance of the item from the bag.
  20.   def remove(self, item) :
  21.     assert item in self._theItems, "The item must be in the bag."
  22.     ndx = self._theItems.index(item)
  23.     return self._theItems.pop(ndx)
  24.        
  25.    # Returns an iterator for traversing the list of items.
  26.   def __iter__(self, item) :
  27.     ......

A list stores references to objects and technically would be illustrated as shown in the figure below.

To conserve space and reduce the clutter that can result in some of the figures, however, we illustrate objects in the text as boxes with rounded edges and show them stored directly within the list structure. Variables will be illustrated as square boxes with a bullet in the middle and the name of the variable printed nearby.

Special Topic
Mutable versus Immutable Objects
Page last modified on August 02, 2023, at 04:47 PM