summaryrefslogtreecommitdiff
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-11-05 13:35:26 -0700
committerRaymond Hettinger <python@rcn.com>2011-11-05 13:35:26 -0700
commit4e6bf41934de08f62b22b90cda2e331eb4641dea (patch)
tree6fd3f1a8069eccab24e1d38fe5e093f9056ffa4e /Lib/collections.py
parent0115fae8c21122cda699d2d9fbf51f755bb7b365 (diff)
downloadcpython-git-4e6bf41934de08f62b22b90cda2e331eb4641dea.tar.gz
Improve Counter.__repr__() to not fail with unorderable values
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py8
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 2b6abd879f..d2625fe03e 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -583,8 +583,12 @@ class Counter(dict):
def __repr__(self):
if not self:
return '%s()' % self.__class__.__name__
- items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
- return '%s({%s})' % (self.__class__.__name__, items)
+ try:
+ items = ', '.join(map('%r: %r'.__mod__, self.most_common()))
+ return '%s({%s})' % (self.__class__.__name__, items)
+ except TypeError:
+ # handle case where values are not orderable
+ return '{0}({1!r})'.format(self.__class__.__name__, dict(self))
# Multiset-style mathematical operations discussed in:
# Knuth TAOCP Volume II section 4.6.3 exercise 19