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: random.choice should accept a set as input
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Jason.Ward, mark.dickinson, mohd.akram, r.david.murray, rhettinger
Priority: normal Keywords: patch

Created on 2013-03-18 21:47 by mohd.akram, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
17470.patch Jason.Ward, 2013-03-19 04:11 Fix (with a test) for this issue review
Messages (4)
msg184532 - (view) Author: Mohammad Akram (mohd.akram) Date: 2013-03-18 21:47
I think the random.choice function should support sets. The random.sample function already supports sets and therefore the API should be consistent in this regard.

This is how it's done in the sample method (line 295):

        if isinstance(population, _Set):
            population = tuple(population)
msg184589 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-03-19 04:19
Choice is a very simple function.  If it supported sets, it would have to convert the set to a sequence *each time* choice was called.  It is probably better to leave this as a task for the user of choice to perform.  Sample, on the other hand, does quite a bit of work after doing the conversion, and one is somewhat less likely to call it multiple times on the same input.

I'm not going to close the issue, since my argument here speaks to the implementation rather than the API, so I'm not entirely comfortable with it.  I'll let Raymond decide :).
msg184599 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-03-19 05:42
Sorry Mohammad, I concur with David Murray's reasoning.
msg185953 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-04-03 20:55
See also issue #7522 (with an identical title!) for previous discussion.
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61672
2013-04-03 20:55:02mark.dickinsonsetnosy: + mark.dickinson
messages: + msg185953
2013-03-19 05:42:44rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg184599
2013-03-19 04:54:08Jason.Wardsetnosy: + Jason.Ward
2013-03-19 04:19:44r.david.murraysetnosy: + rhettinger, r.david.murray
messages: + msg184589
2013-03-19 04:11:02Jason.Wardsetfiles: + 17470.patch
keywords: + patch
2013-03-18 21:47:12mohd.akramcreate