Message338463
> 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}) |
|
Date |
User |
Action |
Args |
2019-03-20 12:39:30 | xtreak | set | recipients:
+ xtreak, rhettinger, Nikita Smetanin |
2019-03-20 12:39:30 | xtreak | set | messageid: <1553085570.26.0.596800882167.issue36380@roundup.psfhosted.org> |
2019-03-20 12:39:30 | xtreak | link | issue36380 messages |
2019-03-20 12:39:30 | xtreak | create | |
|