homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [].sort() should return self
Type: enhancement Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: dwt, ezio.melotti
Priority: normal Keywords:

Created on 2012年01月17日 10:21 by dwt, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (4)
msg151439 - (view) Author: Martin Häcker (dwt) Date: 2012年01月17日 10:21
[].sort() returns None which means you can't chain it.
So for example someDict.keys().sort()[0] doesn't work but you have to use sorted(someDict.keys())[0] instead which is harder to read as you have to read the line not from the beginning to the end but jump back and forth in it to understand it (which gets progressively harder as the individual parts of it get longer / more complex).
msg151440 - (view) Author: Martin Häcker (dwt) Date: 2012年01月17日 10:22
It really should return self.
msg151441 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012年01月17日 10:24
This is by design.
Methods that mutate the object return None.
Methods that create a new object return the new object.
list.sort sorts the list in place, so it returns None.
msg151442 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2012年01月17日 10:28
See also http://docs.python.org/faq/design.html#why-doesn-t-list-sort-return-the-sorted-list 
History
Date User Action Args
2022年04月11日 14:57:25adminsetgithub: 58013
2012年01月17日 10:28:59ezio.melottisetmessages: + msg151442
2012年01月17日 10:24:35ezio.melottisetstatus: open -> closed

nosy: + ezio.melotti
messages: + msg151441

resolution: rejected
stage: resolved
2012年01月17日 10:22:07dwtsetmessages: + msg151440
2012年01月17日 10:21:51dwtcreate

AltStyle によって変換されたページ (->オリジナル) /