Intercept and Tag STDOUT values

Andrew Durdin adurdin at
Wed Oct 13 07:29:58 CEST 2004

Further to my last reply: perhaps a better solution overall would be
to create a class (let's call it TaggedFile) that supports the
appropriate methods for file-like behaviour, and wraps any file
(keeping a reference to it in the "wrapped_file" attribute) with the
tagging you want. Then to do the actual redirection:

tagged_stdout = TaggedFile(sys.stdout)
sys.stdout = tagged_stdout
# other code here
sys.stdout = tagged_stdout.wrapped_file

It will then be much clearer that the redirection is occurring.

More information about the Python-list mailing list