add an indent to a stream

Phillip J. Eby pje at
Wed Mar 12 15:03:22 CET 2003

Manuel M Garcia <mail at> wrote in message news:<ld0o6vkta9u7v9un1d1otuvj7nste92ni2 at>...
> I often need to add an indent to a stream, usually so the output of
> pprint or traceback is indented so I can tell debugging output from my
> program's actual output.
> Below is a class to help do this.  I think I got the details right.
> The 'print' statement, when used with commas, is more complicated than
> I thought.

If you need a generally indentable/outdentable stream, you might try
the class documented here:

The code is at:

IndentedStream is intended for generating Python or XML output, so it
manages a stack of indent levels which can be pushed and popped.  It
also does its indentation lazily, so that if you output a newline and
then change the indent level, it won't have indented the new line yet
unless something was written to the new line.

More information about the Python-list mailing list