[Python-ideas] suggestion about the sort() function of the list instance

qhlonline qhlonline at 163.com
Tue Feb 28 20:31:43 EST 2017


My code example is not proper,  Yes,  may be this is better:  list.sort().revers().
Other languages do this differently.  JavaScript may return the sorted, while C++ STL  returns nothing.
I think that it maybe more important to let user have good knowledge about this function then to have fluent code on some occasions. 
I prefer to draw back this suggestion.  


Regards!

At 2017-03-01 08:13:39, "Steven D'Aprano" <steve at pearwood.info> wrote:
>On Mon, Feb 27, 2017 at 11:07:33AM +0800, qhlonline wrote:
>> Hi, all
>>     I have a suggestion that, the sort() member method of the list 
>>     instance, should return the 'self' as the result of list.sort() 
>>     call. 
>
>Having list.sort() and list.reverse() return self is a perfectly good 
>design. The advantage is you can write things like this:
>
>list.sort().reverse()
>
>but the disadvantage is that it may fool people into thinking it returns 
>a *copy* of the list. Python avoids that trap by returning None, so that 
>you cannot write:
>
>sorted_items = items.sort()
>
>but instead people write:
>
>items = items.sort()
>
>so it seems that whatever we do, it will confuse some people.
>
>
>> Now list.sort() returns nothing, so that I can NOT write 
>> code like this:
>> 
>>     res =  {item: func(item) for item in item_list.sort()}
>
>What is the purpose of the sort? Because dicts are unordered, the 
>results will be no different if you just write:
>
>    d = {item: func(item) for item in item_list}
>
>
>-- 
>Steve
>_______________________________________________
>Python-ideas mailing list
>Python-ideas at python.org
>https://mail.python.org/mailman/listinfo/python-ideas
>Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170301/7c6e6d27/attachment-0001.html>


More information about the Python-ideas mailing list