Is `wsample` a proper name for a function like this?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Oct 11 02:29:37 EDT 2012


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.


-- 
Steven



More information about the Python-list mailing list