[Python-bugs-list] [ python-Feature Requests-547481 ] In-place reverse() and sort() for slices

noreply@sourceforge.net noreply@sourceforge.net
Tue, 23 Apr 2002 15:17:15 -0700


Feature Requests item #547481, was opened at 2002-04-23 09:48
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=547481&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Bernhard Bender (bbender)
Assigned to: Nobody/Anonymous (nobody)
Summary: In-place reverse() and sort() for slices

Initial Comment:
In order to apply sort() or reverse() to only part of a
sequence (a slice), the following code seems to be
necessary:

	# a is a sequence
	tmp = s[m:n]
	tmp.sort()		# or tmp.reverse()
	s[m:n] = tmp

It would be much nicer to apply sort() or reverse() to
the slice directly:

	a[m:n].sort()

However, this does not work, since the slice returns a
new sequence object, on which sort() or reverse() will
work, instead of working on the slice in-place.
After sort() is done, the modified object will be
discarded, since it cannot be assigned to anything as
sort() and reverse() do not have a return value.

Therefore, the line above is accepted by the
interpreter even though it is completely useless.

My proposal is to change the semantics of applying
sort() and reverse() to slices such as to work in-place
on the slice, in stead of working on the new object
created by the slicing operation.

This would be similar to slice assignment, which also
does not create a new object.

Would this change require a PEP?

Bernhard Bender

----------------------------------------------------------------------

>Comment By: Neil Schemenauer (nascheme)
Date: 2002-04-23 22:17

Message:
Logged In: YES 
user_id=35752

This would take some deep magic to make work and would
be a fundamental change to the language.  lst[m:m] is an
expression that returns a new object.  The chances of this
change being accepted is about zero.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=547481&group_id=5470