[Python-Dev] pprint(iterator)

Steven D'Aprano steve at pearwood.info
Thu Jan 29 23:42:15 CET 2009


Michael Foord wrote:

> Don't we have a pretty-print API - and isn't it spelled __str__ ?

Not really. If it were as simple as calling str(obj), there would be no 
need for the pprint module. In any case, it seems that the pprint module 
actually calls repr() on objects other than dicts, tuples and lists.

I'm concerned about the number of special methods exploding, but I've 
also come across times where I needed more than two string 
representations of an object. Sometimes I solved this by adding a 
pprint() method, other times I used other names, and it would be nice if 
there was a standard way of spelling it. So I'm +0.5 on Aahz's 
suggestion of __pprint__.

In my ideal world, __pprint__ should allow (but not require) extra 
arguments, so that one can do something like the following:

pprint(binarytree)  # sensible defaults
pprint(binarytree, order='preorder')



-- 
Steven



More information about the Python-Dev mailing list