[Python-ideas] collections.Counter multiplication

David Mertz mertz at gnosis.cx
Thu May 30 07:48:05 CEST 2013


On Wed, May 29, 2013 at 10:24 PM, Andrew Barnert <abarnert at yahoo.com> wrote:

> On May 29, 2013, at 20:31, David Mertz <mertz at gnosis.cx> wrote:
>
> Well... I actually *did* implement it a few notes up-thread, it's not
> merely simple, but actual.
>
> I think I'd use numbers.Integral and collections.abc.Mapping rather than
> int and Counter. And maybe take any Container as a counter with the value 1
> for each key, given that Counter is intended to work as a multiset.
>

It all still seems somewhat silly to me since the use-case eludes me.
But...

You are right about numbers.Integral, that is a better isinstance() test.
However, I don't think so with collections.abc.Mapping--in that case I
deliberately chose collections.Counter because the semantics seemed
undefined in other cases.

That is, I have no idea what meaning the OP would assign to:

>>> Counter({'a':1,'b':2}) * OrderedDict((('a','x'),('b','y')))

Also, I think the one-liner dict comprehension implementation is simpler
> than the explicit loop around setitem.
>

Yeah, that's probably simpler.  Maybe less explicit, but the code is so
short in any case.


> And I think if you have mult you'll probably want imult as well.
>

Maybe.  It wasn't in the original "spec" so who knows.  But that's
straightforward also, of course.


> But bikeshedding aside, your implementation should be more than enough for
> the OP.
>

That's my sense.

Well, given that it will be in a separate module if the OP puts it on PyPI,
> I don't see any downside to giving it the same name.
>

Fair enough.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130529/24277ded/attachment.html>


More information about the Python-ideas mailing list