Improve Counter.__repr__() to not fail with unorderable values

This commit is contained in:
Raymond Hettinger 2011-11-05 13:35:26 -07:00
parent 0115fae8c2
commit 4e6bf41934
2 changed files with 12 additions and 2 deletions

View file

@ -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