Rebinding stdout (was: Re: Python! Is! Truly! Amazing!)

Just just at xs4all.nl
Mon Jan 3 04:08:46 EST 2005


In article <Xns95D365EACCBD7JanC at 213.224.134.77>,
 JanC <usenet_spam at janc.invalid> wrote:

> Something like this:
> 
> py> import cStringIO
> py> import sys
> py>
> py> def foo():
> ...     print "test"
> ...
> py> f = cStringIO.StringIO()
> py> sys.stdout = f
> py> foo()
> py> s = f.getvalue()
> py> sys.stdout = sys.__stdout__

You should always save stdout instead of using __stdout__. It may not be 
the same! I don't think anyone should *ever* use __stdout__ except when 
debugging. See also:

  http://mail.python.org/pipermail/python-dev/2000-October/010144.html

> py> f.close()
> py> print s.capitalize()
> Test

Just



More information about the Python-list mailing list