On Mon, Aug 3, 2020 at 8:26 AM Ram Rachum <r...@rachum.com> wrote: > On Mon, Aug 3, 2020 at 3:20 PM <raymond.hettin...@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
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SKBUCZPABJM7B7MEE6IYLQCTOYHZLRUS/ Code of Conduct: http://python.org/psf/codeofconduct/