Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Container __init__, hookiter and purge methods

A container may be empty or it may contain one or more other objects. Typically, a container has finite capacity. As shown in Program gif, a single instance attribute called _count is used to keep track of the number of objects held in the container. The _count instance attribute is set initially to zero. It is the responsibility of the derived class to update this instance attribute as required.

   program4516
Program: Container class.

The purpose of the purge method is to discard all of the contents of a container. This method is declared abstract because the manner in which the purge method is implemented depends on the type of the container. After a container is purged, the value of the _count instance attribute should be zero.

The purpose of the __iter__ method is to return an iterator that enumerates the objects in the container. Iterators are discussed in Section gif.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.