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

alex23 wuwei23 at gmail.com
Thu Oct 11 00:42:14 EDT 2012


On Oct 11, 1:33 pm, Satoru Logic <satorulo... at gmail.com> 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.
>
> So when I saw this `wsample` function, I considered it a improper name.

I tend to agree. I like descriptive names for functions that don't
assume familiarity. If you're unhappy with a function's name and
you're unable to modify the module it belongs to, you can always
rebind it yourself:

    from utils import wsample as weighted_random_choice

If I'm then going to use that function heavily elsewhere, I _might_
rebind it, but always within the scope that is going to use the
abbreviated binding:

    wrc = weighted_random_choice
    <lots of lines of code using wrc>



More information about the Python-list mailing list