[Python-Dev] pprint(iterator)
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