[issue21542] pprint doesn't work well for counters, sometimes shows them like a dict
akira
report at bugs.python.org
Wed May 21 15:26:40 CEST 2014
akira added the comment:
If it fits on a line then it seems Counter's repr is used:
>>> pprint(Counter({i:i*i for i in range(10)}))
Counter({9: 81, 8: 64, 7: 49, 6: 36, 5: 25, 4: 16, 3: 9, 2: 4, 1: 1, 0: 0})
Otherwise It is shown as a dict (Counter is a dict subclass) if it is too
large (multi-line):
>>> pprint(Counter({i:i*i for i in range(10)}), width=20)
{0: 0,
1: 1,
2: 4,
3: 9,
4: 16,
5: 25,
6: 36,
7: 49,
8: 64,
9: 81}
the behaviour is weird but pprint doesn't promise that custom objects such
as Counter that can't be created using Python literals will be printed in a
reversible manner.
It seems there is a special support for some objects:
>>> pprint(frozenset({i for i in range(10)}))
frozenset({0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
>>> pprint(frozenset({i for i in range(10)}), width=20)
frozenset({0,
1,
2,
3,
4,
5,
6,
7,
8,
9})
Perhaps the support for Counter could be added using functools.singledispatch
and/or __prettyprint__ hook from issue #7434
----------
nosy: +akira
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21542>
_______________________________________
More information about the Python-bugs-list
mailing list