A discussion on the py3k list reminded me that translating a forward slice into a reversed slice is significantly less than obvious to many people. Not only do you have to negate the step value and swap the start and stop values, but you also need to subtract one from each of the step values, and ensure the new start value was actually in the original slice: reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step] An rslice builtin would make the latter version significantly easier to read: seq[rslice(start, stop, step)] Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
Nick Coghlan wrote:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
How would this deal with omitted start and/or stop values? -- Greg
On 8/29/06, Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Nick Coghlan wrote:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
How would this deal with omitted start and/or stop values?
More generally, given start/stop/step don't have to be numbers, how can this work in general? I don't actually see a need for this, given that reversed(seq[start:stop:step]) covers all of the real use cases I can think of... Paul.
Greg Ewing wrote:
Nick Coghlan wrote:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
How would this deal with omitted start and/or stop values?
Badly! (negative indices are screwed, too) This would be an awful lot easier if we could just subclass slice, and do the calculation in the indices() method where we have access to len(seq) in order to deal with an omitted stop value and negative indices. Ah well, never mind. I'll take it back to the Py3k list, and see if I can find some small changes we can make so that slice() doesn't feel like a bolted-on hack that exists solely to avoid the need for a new special method :) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
That discussion on py3k is far from finished. We've also had a similar discussion in the past and ended up with reversed(), which solves the problem on the other end (using a forward slice but iterating backwards). Also, when I saw your subject I thought rslice() was related to rfind(), so the name is at best questuinable. Let's drop this until we've got clarity on what Py3k actually will do. --Guido On 8/29/06, Nick Coghlan <ncoghlan@iinet.net.au> wrote:
A discussion on the py3k list reminded me that translating a forward slice into a reversed slice is significantly less than obvious to many people. Not only do you have to negate the step value and swap the start and stop values, but you also need to subtract one from each of the step values, and ensure the new start value was actually in the original slice:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
Cheers, Nick.
-- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
Nick Coghlan wrote:
A discussion on the py3k list reminded me that translating a forward slice into a reversed slice is significantly less than obvious to many people. Not only do you have to negate the step value and swap the start and stop values, but you also need to subtract one from each of the step values, and ensure the new start value was actually in the original slice:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
Or slice.reversed(). -- David Hopwood <david.nospam.hopwood@blueyonder.co.uk>
On Tue, 29 Aug 2006 17:44:40 +0100, David Hopwood <david.nospam.hopwood@blueyonder.co.uk> wrote:
Nick Coghlan wrote:
A discussion on the py3k list reminded me that translating a forward slice into a reversed slice is significantly less than obvious to many people. Not only do you have to negate the step value and swap the start and stop values, but you also need to subtract one from each of the step values, and ensure the new start value was actually in the original slice:
reversed(seq[start:stop:step]) becomes seq[(stop-1)%abs(step):start-1:-step]
An rslice builtin would make the latter version significantly easier to read:
seq[rslice(start, stop, step)]
Or slice.reversed().
Better, slice.reversed(length). Jean-Paul
participants (7)
-
David Hopwood -
Greg Ewing -
Guido van Rossum -
Jean-Paul Calderone -
Nick Coghlan -
Nick Coghlan -
Paul Moore