Possible improvement to slice opperations.
Terry Reedy
tjreedy at udel.edu
Sun Sep 4 21:11:30 EDT 2005
"Ron Adam" <rrr at ronadam.com> wrote in message
news:FRISe.13089$xl6.6814 at tornado.tampabay.rr.com...
> Slicing is one of the best features of Python in my opinion, but
> when you try to use negative index's and or negative step increments
> it can be tricky and lead to unexpected results.
>
> This topic has come up fairly often on comp.lang.python, and often times,
> the responses include:
> * Beginners should avoid negative extended slices.
> * Slices with negative step increments are for advanced
> python programmers.
> * It's not broke if you look at it in a different way.
> * You should do it a different way.
You omitted the slice-specific response I gave before and repeat here with
more detail by quoting from What's New in Python 2.3.
http://www.python.org/doc/2.3/whatsnew/section-slices.html
"
15 Extended Slices
Ever since Python 1.4, the slicing syntax has supported an optional third
``step'' or ``stride'' argument. For example, these are all legal Python
syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the
request of the developers of Numerical Python, which uses the third
argument extensively. However, Python's built-in list, tuple, and string
sequence types have never supported this feature, raising a TypeError if
you tried it.
"
Again, extended slices were probably designed by and certainly designed for
Numerical Python and for 7 years were used at least mainly by Numerical
Python. They were not designed for other users like you. The responses
you summarized and distain pretty much all derive from this history.
So, I am pretty sure that changes to the core would have to be upwards
compatible with current usage. On the other hand, your nxlist subclass of
list seems to work pretty well now.The 2.2+ ability to do this sort of
thing is what type subclassing was made for.
Or one could just write an extended slice function similar to your .normslc
method. I might even use such a thing one day.
Terry J. Reedy
More information about the Python-list
mailing list