[Python-Dev] inline sort option

Guido van Rossum guido at python.org
Thu Oct 16 14:03:26 EDT 2003


> Are you proposing something like:
>> print mylist.sort(inplace=False) # prints a new, sorted list while
> # leaving the original list intact
>>> which would be implemented something like this:
>> def inlinesort(alist, *args, **kwds):
> newref = alist[:]
> newref.sort(*args, **kwds)
> return newref
>>> If that is what you're after, I think it is a good idea. It avoids the
> perils of mutating methods returning self. It is explicit and pleasing
> to write:
>> for elem in mylist.sort(inplace=False):
> . . .
>> It is extra nice in a list comprehension:
>> peckingorder = [d.name for d in duck.sort(key=seniority,
> inplace=False)]
>> Instead of "inplace=False", an alternative is "inline=True".

*If* we're going to consider this, I would recommend using a different
method name rather than a keyword argument. Arguments whose value
changes the return type present a problem for program analysis tools
like type checkers (and IMO are also easily overseen by human
readers). And, it's easier to write l.sorted() rather than
l.sort(inline=True).
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /