list.sort()

Rikard Bosnjakovic bos at hack.org
Sun Jun 17 13:42:09 EDT 2001


I know the sort-method on lists changes the list in-place and doesn't
return anything, but I'm still confused.

Take these examples:

>>> l = [4,3,2]
>>> l = l
>>> l
[4, 3, 2]

The list is unchanged, even after assigning it to itself. But:

>>> l = l.sort()
>>> l
>>> print l
None

If we split the problem into parts, "l.sort()" sorts l and it becomes
[2,3,4]. I expected it to be [2,3,4] after assigning it to itself (after
it got sorted), but the list seem to got deleted instead. I don't like
this behaviour, at all.

Why does Python behave like this? Why does sort() change lists
_in-place_, instead of returning a list?


-- 
Rikard Bosnjakovic - http://bos.hack.org/cv/ - ICQ: 1158217

Anyone sending unwanted advertising e-mail to my address will be
charged $250 for network traffic and computing time. By extracting my
address from this message or its header, you agree to these terms.



More information about the Python-list mailing list