Raymond Hettinger <[email protected]> added the comment:
FWIW, the promised semantics of saturating arithmetic require that
_keep_positive be run on entire the entire counter:
>>> c1 = Counter(a=-3, b=4)
>>> +c1
Counter({'b': 4})
>>> from collections import Counter
>>> c1 = Counter(a=-3, b=4)
>>> c2 = Counter(b=5, c=5)
>>> c1 += c2 # The "a" entry gets removed
>>> c1
Counter({'b': 9, 'c': 5})
When this behavior isn't wanted, use the update() method which is documented
not to perform removal of non-positive values. That method is fast in pure
python and has an even faster C helper function:
>>> c1 = Counter(a=-3, b=4)
>>> c2 = Counter(b=5, c=5)
>>> c1.update(c2)
>>> c1
Counter({'b': 9, 'c': 5, 'a': -3})
And as mentioned before, the Counter API is not encapsulated -- the underlying
structure is fully exposed, so a user is free to do anything with it that they
can do with a regular dictionary.
A final thought is to be careful when proposing to rewrite the internals of the
existing methods. These methods are careful to retain ordering of inputs, to
not use auxiliary data fields (it is just a dict variant with no other
structure), and to make only minimal assumptions about the datatype for the
count. They are designed to not be magical.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36380>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com