[Python-Dev] pprint(iterator) (original) (raw)

Matthew Wilkes matthew at matthewwilkes.co.uk
Mon Feb 2 21:16:11 CET 2009


On 29 Jan 2009, at 21:54, Nick Coghlan wrote:

For the "reiterable" cases like dictionary views (where the object is not consumed), an appropriate str or repr should be written).

Indeed, instead of having a pprint why not just allow a repr
to reformat its output?

dict having: def repr(self, pretty=False): if pretty: return "{\n a: 1\n b: 2\n}" else: return "{a: 1, b: 2}"

That way you can specify your own pretty format, intending it to still
be a valid repr and pprint can do:

try: print(obj.repr(pretty=True)) except TypeError: print(prettify(repr(obj)))

That way it's opt in, doesn't have a special method, and includes the
mental prompt "this should eval() to obj"

Matt



More information about the Python-Dev mailing list