Is `wsample` a proper name for a function like this?
suzaku
satorulogic at gmail.com
Thu Oct 11 02:44:42 EDT 2012
On Thursday, October 11, 2012 2:29:37 PM UTC+8, Steven D'Aprano wrote:
> On Wed, 10 Oct 2012 20:33:16 -0700, Satoru Logic wrote:
>
>
>
> > I came across a function named `wsample` in a `utils` package of my
>
> > workplace recently.
>
> >
>
> > The "w" in `wsample` stands for `weighted`, and it randomly selects an
>
> > element from a container according to relative weights of all the
>
> > elements.
>
> >
>
> > In most articles and codes I saw online, a function like this is often
>
> > named `weighted_random_choice`, which sounds *correct* to me. So when I
>
> > saw this `wsample` function, I considered it a improper name. Because
>
> > `wsample`makes me think of `random.sample`, which returns a list of
>
> > randomly generated elements, not a element.
>
>
>
> You can have a sample size of one.
>
>
>
> wsample sounds fine to me. weighted_random_choice is okay too. It depends
>
> whether you value brevity over explicitness. Explicit is good, but some
>
> names are just too long.
>
>
>
> If this were my code base, I would probably go for weighted_sample
>
> without mentioning "random" in the name, the reasoning being that samples
>
> are almost always random so explicitly saying so doesn't help much.
I think if a programmer has used the built-in `random` module before, he would expect a function with "sample" in its name to return a population sequence.
If a function is to return scalar value instead of sequence, I would expect it to be named "choice".
>
>
>
>
>
> --
>
> Steven
More information about the Python-list
mailing list