a question regarding conciseness

Magnus Lyckå magnus at thinkware.se
Thu Feb 21 07:16:48 EST 2002


Paul Rubin wrote:

> But there's no reason sort couldn't sort in place and return the list.
> d.keys() already makes a temporary list with the keys in it after all.
> The temporary list can be sorted in place without disturbing the
> dictionary.


But the dictionary is just a special case. Sort sorts any list.

You should ask Guido, but I think it's made like this to reduce
confusion. It DOES an in-place sort. If

 >>> a = [1,5,3]
 >>> b = a.sort()
 >>> print b
[1, 3, 5]

worked, it would not be obvious that both a and b now refered to the
same, now sorted list. Like this:

>>> a = [1,5,3]

 >>> b = a

>>> b = b.sort()
>>> print b
[1, 3, 5]

Most people would probably think that it was equivalent with:

 >>> a = [1,5,3]
 >>> b = a[:]
 >>> b = b.sort()
 >>> print b
[1, 3, 5]

But if it was implemented like that, all sorts would use much more
memory.




More information about the Python-list mailing list