
On Mon, Aug 3, 2020 at 8:26 AM Ram Rachum <ram@rachum.com> wrote:
On Mon, Aug 3, 2020 at 3:20 PM <raymond.hettinger@gmail.com> wrote:
Ram Rachum wrote:.
I notice that the random.sample function doesn't have a default behavior set when you don't specify k. This is fortunate, because we could make that behavior just automatically take the length of the first argument. So we could do this: shuffled_numbers = random.sample(range(10, 10 ** 5)) What do you think?
This is bad API design. The most likely user mistake is to omit the *k* argument. We want that to be an error. It is common to sample from large populations, we don't want the default to do anything terrible — for example, you're in a Jupyter notebook and type "sample(range(10_000_000))" and forget to enter the sample size.
Also, having *k* default to the population size would be surprisingly inconsistent given that choices() has a default k=1. API design principle: don't have unexpectedly different defaults in related functions.
Hmm, yes, I agree with both these points.
I do think that `sample(x, k=len(x))` is cumbersome when `x` is not a variable but defined inline. But I guess I'll let this one go.
I've found it cumbersome in the past myself, but an easy way around that now is the walrus: `sample(_:=[1,2,3], len(_))` --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler