
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. Lastly, the use for in-line shuffling is not the primary use case. If there were a default argument, it should cater to the principal use case,. API design principle: don't do anything weird or unexpected by default. IMO you're trying too hard to jam a round peg into a square hole. There isn't a substantive problem being solved — being explicit by writing "sample(p, len(p))" instead of "sample(p)" isn't an undue burden. Please also consider that we thought about all of this when sample() was first created. The current API is intentional. As you noted, this suggestion was also already rejected on the bug tracker. So, this thread seems like an attempt to second guess that outcome as well as the original design decision. If you're going to do something like that, save it for something important :-) Raymond