list.sort()
Charlie Dyson
charlie at charliedyson.net
Sun Jun 17 16:13:24 EDT 2001
William Park - opengeometry at yahoo.ca wrote on Sunday 17 June 2001 17:58:
> On Sun, Jun 17, 2001 at 07:42:09PM +0200, Rikard Bosnjakovic wrote:
>> >>> 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.
>
> 'l.sort()' returns 'None', and you are assigning this 'None' back to
> 'l'. Instead, try
> >>> l.sort()
> >>> print l
>
>> Why does Python behave like this? Why does sort() change lists
>> _in-place_, instead of returning a list?
>
> Efficiency.
>
Hmm, I actually think that this behaviour is not very logical. Concider:
x = {'Python' : 'Cool', 'Linux' : 'Cool'}
print x.keys().sort() # Prints None
It would be better if it sort **returned** the results, instead I have to
do:
keys = x.keys()
keys.sort()
That's one extra line of code!!!
Anyway, I get on with Python better than I get on with other languages I
must not mention in order to avert a flame-war.
Charlie Dyson - charlie at charliedyson.net
More information about the Python-list
mailing list