classification
Title: random choice to delegate to sample on sets
Type: Stage: resolved
Components: Library (Lib) Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: Ilya Kamenshchikov, rhettinger
Priority: normal Keywords:

Created on 2020-02-13 12:59 by Ilya Kamenshchikov, last changed 2020-02-14 01:37 by rhettinger. This issue is now closed.

Messages (2)
msg361954 - (view) Author: Ilya Kamenshchikov (Ilya Kamenshchikov) * Date: 2020-02-13 12:59
In a few of my projects I had this (minor) pain of having to remember which collections of elements are sets and which are [list, tuple]. It causes me to double check and have random.sample(my_set, 1)[0] in many places. 

To me this is not how I think and causes friction: conceptually, I know something is a collection and I want 1 random choice from it. Having to differentiate on sequences vs sets makes my code uglier :(

This issue is similar to https://bugs.python.org/issue37708.
msg361979 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2020-02-14 01:37
Thanks for the suggestion.  I think this is close to being a duplicate of 37708 which was rejected.  For the most part, it was mistake that sample() accepted sets.  The implicit conversion has led to surprising performance issues.  If that API was being created from scratch, it would not support sets at and would require the user to explicitly convert to a sequence.  I do not wish to replicate this mistake with choices().

As for delegating to sample(), that doesn't make sense because choices() works with replacement and sample() works without replacement.  The two are semantically different.
History
Date User Action Args
2020-02-14 01:37:27rhettingersetstatus: open -> closed
messages: + msg361979

assignee: rhettinger
resolution: duplicate
stage: resolved
2020-02-13 13:04:49xtreaksetnosy: + rhettinger
2020-02-13 12:59:45Ilya Kamenshchikovcreate