[Python-Dev] extended slicing for lists
Michael Hudson
mwh21@cam.ac.uk
29 Jul 2000 10:07:43 +0100
The patch is here, btw:
http://sourceforge.net/patch/?func=detailpatch&patch_id=100998&group_id=5470
Ka-Ping Yee <ping@lfw.org> writes:
> On 28 Jul 2000, Michael Hudson wrote:
> > >>> l = range(10)
> > >>> l[2:10:2] = [0]*5
> > >>> l
> > [0, 1, 0, 3, 0, 5, 0, 7, 0, 9]
> > >>>
>
> Hmph. I'd be perfectly comfortable with
>
> >>> l[2:10:2] = [0]*5
> TypeError: lists cannot assign to stepped slices
>
> I can't see this being a commonly desired feature. If it
> were perfectly clear what should happen in all cases, i'd
> be okay with it -- but the ambiguity you get when the length
> of the right side doesn't match the length of the indicated
> slice is sufficient for me to suggest just dropping it:
> better to avoid confusion altogether.
Well, the BDFL spoke.
> > (and presumably del l[a:b:c])
>
> That seems reasonable and well-defined.
Yes.
> A separate question: does l[::-1] return l reversed?
Yes. All that cleverness is in PySlice_GetIndices, so this patch gets
it for free.
> That is, are the defaults for omitted start/end exchanged if step is
> negative? This would seem to be quite useful -- having to say
> l[len(l)-1:-1:-1] would be a royal pain.
Well, you could probably always say
l[sys.maxint:0:-1]
(your example returns the empty list!) but yes, I think l[::-1] is one
of the neater bits of new behaviour.
Cheers,
M.
--
"declare"? my bogometer indicates that you're really programming
in some other language and trying to force Common Lisp into your
mindset. this won't work. -- Erik Naggum, comp.lang.lisp