This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Nikita Smetanin, josh.r, remi.lapeyre, rhettinger, xtreak
Date 2019-03-20.19:07:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1553108827.37.0.380495421927.issue36380@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-03-20 19:07:07rhettingersetrecipients: + rhettinger, josh.r, remi.lapeyre, xtreak, Nikita Smetanin
2019-03-20 19:07:07rhettingersetmessageid: <1553108827.37.0.380495421927.issue36380@roundup.psfhosted.org>
2019-03-20 19:07:07rhettingerlinkissue36380 messages
2019-03-20 19:07:07rhettingercreate