[Python-ideas] Implementing a set of operation (+, /, - *) on dict consistent with linearAlgebrae

David Mertz mertz at gnosis.cx
Tue Oct 30 19:19:59 EDT 2018


Counter doesn't QUITE do the same thing as this `mdict`.  But it's pretty
close.

I think if .__add__() became a synonym for .update() that wouldn't break
anything that currently works.  But I'm probably wrong, and missing a case
in my quick thought:

>>> from collections import Counter
>>> c = Counter(a=[2], b='a')
>>> c.update(c)
>>> c
Counter({'a': [2, 2], 'b': 'aa'})
>>> c2 = Counter(a=1, b=2)
>>> c2 + c2
Counter({'b': 4, 'a': 2})
>>> c2.update(c2)
>>> c2
Counter({'b': 4, 'a': 2})
>>> c + c
Traceback (most recent call last):
  File "<ipython-input-18-e88785f3c342>", line 1, in <module>
    c + c
  File "/anaconda3/lib/python3.6/collections/__init__.py", line 705, in
__add__
    if newcount > 0:
TypeError: '>' not supported between instances of 'list' and 'int'

On Tue, Oct 30, 2018 at 6:54 PM Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:

> > In [12]: a= mdict(a=[2], b='a')
> > In [13]: a+a
>
> Aren't you reinventing the Counter type?
>
> >>> from collections import Counter
> >>> c = Counter(a=1,b=2)
> >>> c + c
> Counter({'b': 4, 'a': 2})
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181030/f0229293/attachment.html>


More information about the Python-ideas mailing list