Issue 13805: [].sort() should return self (original) (raw)

Issue13805

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:25 admin set github: 58013
2012-01-17 10:28:59 ezio.melotti set messages: +
2012-01-17 10:24:35 ezio.melotti set status: open -> closednosy: + ezio.melottimessages: + resolution: rejectedstage: resolved
2012-01-17 10:22:07 dwt set messages: +
2012-01-17 10:21:51 dwt create