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

__init__ Method

 

Program gif gives the code for the Array class __init__ method. The __init__ method takes three arguments, self, length and baseIndex. The length argument gives the desired array length and the baseIndex argument gives the lower bound for array indices. The __init__ method creates list of of the desired length and then sets the _baseIndex. Note that the default base index is zero and the default array length is zero.

   program2707
Program: Array class __init__ method.

In Python, when a list is allocated, two things happen. First, memory is allocated for the list object and its elements. Second, each element of the list is initialized with the appropriate default value (in this case all of the list elements refer to the None object).

For now, we shall assume that the first step takes a constant amount of time. Since there are tex2html_wrap_inline60415 elements to be initialized, the second step takes O(n) time. Therefore, the running time of the Array class __init__ method is O(n).


next up previous index

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