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

Tim Peters tim.peters at gmail.com
Wed Apr 18 12:05:43 EDT 2018


[Raymond]
> I've started working on an implementation and several choices arise:
>
> 1) Reject scalar with a TypeError if scalar is a Counter
> 2) Reject scalar with a TypeError if scalar is a Mapping
> 3) Reject scalar with a TypeError if scalar is a Collection
> 4) Reject scalar with a TypeError if scalar is Sized (has a __len__ method).
>
> I lean toward rejecting all things Sized because _everyone_ knows that scalars aren't sized ;-)

Hard to know how gonzo to get :-(

_Scalar = (Sized, Container, Iterable)): # has __len__, __getitem__, or __iter__
...
if isinstance(arg, _Scalar):
    raise TypeError ...

would also reject things like generator expressions.  But ... those
would blow up anyway, when multiplication was attempted.

So, ya!  Sticking to Sized sounds good :-)


More information about the Python-ideas mailing list