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.