Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:
> It also unclear if there's even a need to check for non-positives with > _keep_positive in ops like __iadd__, __iand__ and __ior__ (except __isub__) > as it expects Counters which are always positive. Counter accepts dictionaries and keyword arguments where keys can have negative or zero as values where _keep_positive needs to be checked for add operation too. >>> Counter(a=3) + Counter(a=-1) Counter({'a': 2}) Counter can have keys where the value is already 0 during construction that are not deleted and thus during __iadd__ all the keys have to be checked for zero. Below is the behavior in Python 3.7 and checking only for changed "b" key for non-zero in the example would leave "a" key also in the result with the optimization proposed? $ python3.7 Python 3.7.1rc2 (v3.7.1rc2:6c06ef7dc3, Oct 13 2018, 05:10:29) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from collections import Counter >>> a = Counter(a=0, b=1) >>> a Counter({'b': 1, 'a': 0}) # Not removed during construction >>> b = Counter(b=1) >>> a += b >>> a Counter({'b': 2}) ---------- nosy: +xtreak _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue36380> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com