[Python-ideas] collections.Counter should implement __mul__, __rmul__
Raymond Hettinger
raymond.hettinger at gmail.com
Mon Apr 16 00:39:32 EDT 2018
> On Apr 15, 2018, at 9:04 PM, Peter Norvig <peter at norvig.com> wrote:
>
> it would be a bit weird and disorienting for the arithmetic operators to have two different signatures:
>
> <counter> += <counter>
> <counter> -= <counter>
> <counter> *= <scalar>
> <counter> /= <scalar>
>
> Is it weird and disorienting to have:
>
> <str> += <str>
> <str> *= <scalar>
Yes, there is a precedent that does seem to have worked out well in practice :-) It isn't exactly parallel because strings aren't containers of numbers, they don't have & and |, and there isn't a reason to want a / operation, but it does suggest that signature variation might not be problematic.
BTW, do you just want __mul__ and __rmul__? If those went in, presumably there will be a request to support __imul__ because otherwise c*=3 would still work but would be inefficient (that was the rationale for adding inplace variants for all the current arithmetic operators). Likewise, presumably someone would legitimately want __div__ to support the normalization use case. Perhaps less likely, there would be also be a request for __floordiv__ to allow exactly scaled results to stay in the domain of integers. Which if any of these makes sense to you?
Also, any thoughts on the cleanest way to express the computation of a chi-squared statistic (for example, to compare observed first digit frequencies to the frequencies predicted by Benford's Law)? This isn't an arbitrary question (it came up when a professor first proposed a variant of this idea a few years ago).
Raymond
More information about the Python-ideas
mailing list