summaryrefslogtreecommitdiffstats
path: root/Lib/collections/__init__.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-08-09 20:00:40 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-08-09 20:00:40 (GMT)
commitfcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4 (patch)
tree51c5d45f70cd9946a373083bd31ddc2f109a9647 /Lib/collections/__init__.py
parent18205baf251495b5a54b08c1f9b0e1763eb27aa1 (diff)
downloadcpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.zip
cpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.tar.gz
cpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.tar.bz2
Add support for unary plus and unary minus to collections.Counter()
Diffstat (limited to 'Lib/collections/__init__.py')
-rw-r--r--Lib/collections/__init__.py11
1 files changed, 11 insertions, 0 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 03bd2b3..2007e68 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -672,6 +672,17 @@ class Counter(dict):
result[elem] = newcount
return result
+ def __pos__(self):
+ 'Adds an empty counter, effectively stripping negative and zero counts'
+ return self + Counter()
+
+ def __neg__(self):
+ '''Subtracts from an empty counter. Strips positive and zero counts,
+ and flips the sign on negative counts.
+
+ '''
+ return Counter() - self
+
########################################################################
### ChainMap (helper for configparser and string.Template)