Negative array indicies and slice()

andrewr3mail at gmail.com andrewr3mail at gmail.com
Mon Oct 29 04:08:28 EDT 2012


On Sunday, October 28, 2012 10:14:03 PM UTC-7, Paul Rubin wrote:
> Andrew writes:
> 
> > So: Why does python choose to convert them to positive indexes, and
> 
> > have slice operate differently than xrange 
> 
> 
> 
> There was a thread a few years back, I think started by Bryan Olson,
> 
> that made the case that slice indexing is a Python wart for further
> 
> reasons than the above, and suggesting a notation like x[$-5] to denote
> 
> what we now call x[-5] (i.e. $ is the length of the string).  So your
> 
> example x[$-4:3] would clearly be the same thing as x[6:3] and not give
> 
> any suggestion that it might wrap around.

I'm getting very frustrated with the editor provided for this group... It keeps posting prematurely, and putting my email in even when I tell it not to each time; and there is no way to edit a post... but deleting is ok...

I think Olson makes a good point.  The len() operator is so ubiquitous that it would be very useful to have a shorthand like that.

I'll have to look for his thread.

I'm thinking that I might just patch my version of Python 3.x, in C, to allow iterators to be passed to __getitem__; I haven't ever seen someone wanting to use mixed sign indexes to extract a small chunk of an array in the middle; so I don't think my patch will break existing code.

The snippets of code given by other posters in the thread might also be used to make a compatibility wrapper; I'll have to study it closer; so that distributed code would still work on unpatched python, albeit much slower.



More information about the Python-list mailing list