examplerange class as the final example for this sectionwe develop our own implementation of class that mimics python' built-in range class before introducing our classwe discuss the history of the built-in version prior to python being releasedrange was implemented as functionand it returned list instance with elements in the specified range for examplerange( returned the list [ howevera typical use of the function was to support for-loop syntaxsuch as for in range( unfortunatelythis caused the instantiation and initialization of list with the range of numbers that was an unnecessarily expensive stepin terms of both time and memory usage the mechanism used to support ranges in python is entirely different (to be fairthe "newbehavior existed in python under the name xrangeit uses strategy known as lazy evaluation rather than creating new list instancerange is class that can effectively represent the desired range of elements without ever storing them explicitly in memory to better explore the built-in range classwe recommend that you create an instance as range( the result is relatively lightweight objectan instance of the range classthat has only few behaviors the syntax len(rwill report the number of elements that are in the given range ( in our examplea range also supports the getitem methodso that syntax [ reports the sixteenth element in the range (as [ is the first elementbecause the class supports both len and getitem it inherits automatic support for iteration (see section )which is why it is possible to execute for loop over range at this pointwe are ready to demonstrate our own version of such class code fragment provides class we name range (so as to clearly differentiate it from built-in rangethe biggest challenge in the implementation is properly computing the number of elements that belong in the rangegiven the parameters sent by the caller when constructing range by computing that value in the constructorand storing it as self lengthit becomes trivial to return it from the len method to properly implement call to getitem ( )we simply take the starting value of the range plus times the step size ( for = we return the start valuethere are few subtleties worth examining in the codeto properly support optional parameterswe rely on the technique described on page when discussing functional version of range we compute the number of elements in the range as max( (stop start step /stepit is worth testing this formula for both positive and negative step sizes the getitem method properly supports negative indices by converting an index - to len(self)- before computing the result