[Python-Dev] pprint(iterator)
Aahz
aahz at pythoncraft.com
Thu Jan 29 15:20:21 CET 2009
On Tue, Jan 27, 2009, Raymond Hettinger wrote:
>
> It is becoming the norm in 3.x for functions to return iterators,
> generators, or views whereever possible.
>
> I had a thought that pprint() ought to be taught to print iterators:
>
> pprint(enumerate(seq))
> pprint(map(somefunc, somedata))
> pprint(permutations(elements))
> pprint(mydict.items())
Along the lines of what others have said: pprint() cannot consume an
unknown iterator. Therefore, you can pretty up the existing output
slightly or special-case certain known iterators. There might also be an
API change to pprint() that allowed it to consume iterators.
The reason I'm chiming in is that I would welcome a PEP that created a
__pprint__ method as an alternative to special-casing. I think that it
would be generically useful for user-created objects, plus once you've
added this feature other people can easily do some of the grunt work of
extending this through the Python core. (Actually, unless someone
objects, I don't think a PEP is required, but it would be good for the
usual reasons that PEPs are written, to provide a central place
documenting the addition.)
This can also be done for Python 2.7, too.
--
Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/
Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
More information about the Python-Dev
mailing list