TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
 Specialized Dev Zones Research Center eBook Library .NET Java C++ Web Dev Architecture Database Security Open Source Enterprise Mobile Special Reports 10-Minute Solutions DevXtra Blogs Slideshow

# The Python 2.5 Goodie Bag: Language Enhancements and Modules : Page 2

## The 2.5 version of Python offered lots of useful enhancements. In this article, you'll learn about some specific modules, as well as performance improvements, that are likely to bring big smiles to the faces of many Python developers.

 by Gigi Sayfan
 Jan 5, 2007
 Page 2 of 5
The '__index__' Method
Slicing is an operation performed on sequences that allows you to extract a subset of the element. The syntax is: sequence[start:stop:step]. 'start' is mandatory, 'stop' defaults to the end of the sequence, and 'step' defaults to 1. When you slice a sequence, Python starts from the start index and returns another sequence (same type as original) that contains all the elements between 'start' and 'stop' in increments of 'step'.

You can use a negative number to count from the end of the sequence too. The step may also be negative, but in this case the start index must be bigger than the stop index resulting in a reverse slice. This used to be the only way to reverse a sequence before the reversed() built-in function was introduced in Python 2.4 (yes, you get some history for the same price).

Now let's break some bread and slice it too.

``````
# prepare a list called bread with 10 integers

# plain slice

# slice using negative indices

# old way of reversing a sequence
``````
Output:
``````
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 10]
[2, 3]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
``````
The indices for start, stop, and step used to be integers or long integers only. This is fine for almost everybody. Why would you want to index into a collection using a different type? You wouldn't. After all, the meaning of an index is a specific location inside the sequence, and locations are always integers. However, NumPy, which is the leading Python scientific computing package, requires it. NumPy is a Python extension that provides a lightning fast multi-dimensional array and various functions, linear algebra operations, and transformations to act on it.

I can hear you thinking: "What's the big deal about arrays? Didn't we have them back in the day in BASIC for the Dragon32?". Well, you didn't have THAT kind of array. Multi-dimensional arrays (tensors) are a crucial building block for many scientific computations. NumPy is a very important and influential package that single-handedly made Python a great success in the scientific community. As evidence of its importance, NumPy is slated for inclusion in the standard Python library at some point.

NumPy uses its own data types (remember the ctypes data types?) to represent integers with higher fidelity than Python's native int and long. These types were not usable for slicing, which is very common in NumPy. The most viable solution was to allow arbitrary types to be used as slicing indices if they define an __index__ method whose return value is int or long. In the following code I defined a few classes with __index__ methods that I use to dice and slice a poor 'bread.'

``````
class One(object):
def __index__(self):
return 1

class Two(object):
def __index__(self):
return 2

class Ten(object):
def __index__(self):
return 10

one = One()
two = Two()
ten = Ten()
print x[one:ten:two]
``````
Output:
``````
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]
``````