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

Tim Peters tim.peters at gmail.com
Mon Apr 16 01:51:20 EDT 2018


[Tim]
>> Adding Counter * integer doesn't bother me a bit, but the definition
>> of what that should compute isn't obvious.


[Raymond]
> Any thoughts on Counter * float?   A key use case for what is being proposed is:
>
>     c *= 1 / c.total

Ah, I thought I had already addressed that, but looks like my fingers
forgot to type it ;-)

By all mean, yes!  Indeed, that strengthens the "argument" for why
`Counter * int` should ignore the signs of the values - if we allow
multiplying by anything supporting __mul__, that clearly says we view
multiplication as being outside the "multiset" view, and so there's no
reason at all to suppress values <= 0.

I also have no problem with inplace operators.  Or with adding
`Counter /= scalar", for that matter.

Perhaps whining could be reduced by rearranging the docs some:
clearly separate operations designed to support the multiset view from
the others.  Then "but that operation makes no sense for multisets!"
can be answered with "so don't use it on multisets - like the docs
told you" ;-)


More information about the Python-ideas mailing list