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: return from random.shuffle
Type: enhancement Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Eugene Yunak, abarry, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-12-05 20:34 by Eugene Yunak, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
shuffle_return.patch Eugene Yunak, 2015-12-05 20:34 patch that adds the return statement review
Messages (3)
msg255971 - (view) Author: Eugene Yunak (Eugene Yunak) * Date: 2015-12-05 20:34
random.shuffle operates on a list, and changes it in place. I think returning a reference to this list, the same one we got in as the argument, is quite useful and makes it possible to use random.shuffle in chained function calls, e.g.:

somelist.append(''.join(shuffle(list('hello'))))
[i for i in shuffle(list(range(10))) if i%2]

I don't see any good arguments against this, and I couldn't think of a reasonable test for this — is it necessary to test whether the returned reference is the same as the one passed in?

I'm open to any discussion or suggestions you might have.
msg255974 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2015-12-05 20:46
While I do see the desire to get your list back for such cases (I would have liked so, too, in some cases), it's inconsistent, as everything else operating in-place returns None.

Plus, having it return None helps you remember that you should keep the reference to your list around. What's more, making it return a value will often make newbies believe that they're two separate lists, and then wonder why the first one was shuffled too.

This is error-prone, and these arguments are why it returns None. It has been discussed in the past -- See http://stackoverflow.com/questions/17649875/why-does-random-shuffle-return-none which is related to your issue, and http://stackoverflow.com/questions/240178/python-list-of-lists-changes-reflected-across-sublists-unexpectedly to see what I mean by confused newbies.
msg255980 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-12-05 21:21
Agreed with all Emanuel has said.
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 69997
2015-12-05 21:21:15serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg255980

resolution: rejected
stage: resolved
2015-12-05 20:46:28abarrysetnosy: + abarry
messages: + msg255974
2015-12-05 20:34:19Eugene Yunakcreate