[Python-bugs-list] [ python-Feature Requests-547481 ] In-place reverse() and sort() for slices
noreply@sourceforge.net
noreply@sourceforge.net
Tue, 23 Apr 2002 02:48:28 -0700
Feature Requests item #547481, was opened at 2002-04-23 11: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
----------------------------------------------------------------------
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=355470&aid=547481&group_id=5470