From: Robert K. <rob...@gm...> - 2006-05-30 20:17:39
|
David Huard wrote: > Just a thought: > would it be possible to overload the array __getitem__ method ? > > I can do it with lists, but not with arrays... > > For instance, > > class fortarray(list): > def __getitem__(self, index): > return list.__getitem__(self, index+5) > > and >>>> l = fortarray() >>>> l.append(1) >>>> l[-5] > 1 > > There is certainly a more elegant way to define the class with the > starting index as an argument, but I didn't look into it. For arrays, > this doesn't work out of the box, but I'd surprised if there was no way > to tweak it to do the same. One certainly could write a subclass of array that handles arbitrarily-based indices. On the other hand, writing a correct and consistent implementation would be very tricky. On the gripping hand, a quick hack might suffice if one only needed to use it locally, like inside a single function, and convert to and from real arrays at the boundaries. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |