strings and sort()
Sean 'Shaleh' Perry
shalehperry at attbi.com
Thu Feb 21 01:58:35 EST 2002
On 21-Feb-2002 Paul Rubin wrote:
> "Sean 'Shaleh' Perry" <shalehperry at attbi.com> writes:
>> > Why doesn't sort() return the sorted list. I would like to chain it
>> > to other operations:
>> > b=[x for x in a].sort()
>> >
>> efficiency. Why return the sequence when we usually just need to sort the
>> list. Sort is also a meothod of an object, why should it return a new
>> object
>> instead of just affecting the one it belongs to?
>
> I don't understand this reply. The sort method would be no less efficient
> if it returned the sequence:
>
> def nsort(a):
> a.sort()
> return a
>
of course it would. returning has a cost. Most uses of sort() just need to
in place update the list. If there was actually a copy made as some people
expect that would be even more costly.
When programming in a language, you learn the idioms of that language.
More information about the Python-list
mailing list