[Python-ideas] collections.Counter should implement __mul__, __rmul__

Tim Peters tim.peters at gmail.com
Wed Apr 18 21:48:07 EDT 2018


[Serhiy]
> Isn't everyone expect that x*2 == x + x? Isn't this the definition of
> multiplication?

Note:  in my first message in this thread, I made the same objection.
And that, e.g., it would also be weird if x*-1 is not the same as -x.

Obviously, I don't care enough about those to make up a meaning for
"multiset * scalar" that preserves those in all cases.  And we have to
make up a meaning, because there is no generally accepted
_mathematical_ meaning for what "multiset times a scalar" should do.
It's not that there's controversy about what it should do, it's that
nobody asks the question.  It's like asking "what should the logarithm
of matrix to a base that's a string mean?" This isn't Perl ;-)

For a scalar that's a non-negative integer, I agree repeated addition
makes _most_ sense.  But, as I also noted in my first message, Peter's
"obvious" implementation satisfies that!  Provided that the Counter
represents a legit (all values are strictly positive integers)
multiset to begin with, `Counter * n` is the same as adding the
Counter n times.  If the Counter doesn't represent a legit multiset to
begin with, who cares?  It's not "a multiset operation" at all then.
Or if the scalar isn't a non-negative integer.  What on Earth is
"multiset * math.pi" supposed to do that gives a legit multiset
result?  For each value v, return math.floor(v * math.pi)?  That's
just nuts - _any_ answer to that would be nuts, utterly arbitrary just
to preserve a useless notion of "consistency".

The use cases Peter have in mind appear to do with manipulating
discrete probability distributions represented as Counters, which
really have nothing to do with multisets.  Has values will always be
non-negative, but almost never integers, so almost never _sanely_
viewed as being multisets.

Counter doesn't care.  And I don't want to get in his way by insisting
on some notion of formal consistency in new operations that have
nothing to do with multisets except by accident, or by artificially
forced contrivance.

I've used Counters to represent multisets a fair bit, but have never
had the slightest desire to use "times an integer" as a shorthand for
repeated addition, and can't even dream up a meaning for what dividing
a multiset by an integer (let alone a float!) could return that would
make a lick of sense.

"Scalar broadcast" makes instant sense in both cases, though, just by
dropping the illusion that people using Counters _as multisets_ are
interested in these operations at all.


More information about the Python-ideas mailing list