test script: --------------------------------------- from collections import Counter empty_counter = Counter() counter = Counter('abbc') empty_counter &= 5 counter &= 5 --------------------------------------- results: --------------------------------------- Traceback (most recent call last): File "blah.py", line 5, in counter &= 5 File "/home/ethan/source/python/issue22778/Lib/collections/__init__.py", line 780, in __iand__ other_count = other[elem] TypeError: 'int' object is not subscriptable ---------------------------------------- As can be seen, the error does not show up when the Counter is empty, which could lead to hard to diagnose bugs.
Additional check is not for free. $ ./python -m timeit -s "from collections import Counter; a = Counter(); b = Counter(range(10))" -- "a &= b" Unpatched: 100000 loops, best of 3: 8.4 usec per loop Patched: 100000 loops, best of 3: 9.7 usec per loop