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

__iter__ Method

As discussed in Section gif, the __iter__ method of a Container returns an Iterator. In Python, an iterator is implicitly used when you write a for loop like this:

stack = new StackAsArray(57)
for obj in stack:
    print obj
The Python for loop given above is implemented as if it was coded like this:
it = iter(stack) # Equivalent to stack.__iter__()
while true:
	obj =
	print obj
    except StopIteration:
This code creates an instance of the StackAsArray class and assigns it to the variable stack. Next, several ints are pushed onto the stack. Finally, an iterator is used to systematically print out all of the objects in the stack.

Program gif defines the __iter__ method of the StackAsArray class. The __iter__ method returns a new instance of the nested class StackAsArray.Iterator that implements the iterator protocol (lines 3-14).

Program: StackAsArray class __iter__ method.

The Iterator class has two instance attributes, _container and _position. The _container instance attribute refers to the stack whose elements are being enumerated. The _position instance attribute is used to keep track of the position in the array of the current object.

The _next method is called in the body of the loop to advance the iterator to the next object in the stack. The _next method increments the _position instance attribute and then returns the object in the stack specified at that position. The _next method resets the position to -1 and raises a StopIteration exception when there are not more elements. Clearly, the running time of _next is O(1).

next up previous index

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