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

Ron Garret rNOSPAMon at
Sun Jan 2 15:15:46 EST 2005

In article <cr9ebu$re1$1 at>, aahz at (Aahz) 

> In article <roy-D9F00D.12570402012005 at>,
> Roy Smith  <roy at> wrote:
> >In article <rNOSPAMon-35B48E.09261402012005 at>,
> > Ron Garret <rNOSPAMon at> wrote:
> >> In article <1104657461.868175.252380 at>,
> >>  "Erik  Bethke" <erikbethke at> wrote:
> >>> 
> >>> I have NEVER experienced this kind of programming joy.
> >> 
> >> Just wait until you discover Lisp!
> Taking this more seriously than it deserves, I've tried poking at Lisp a
> couple of times -- each time, I walk away shaking my head in disgust.
> Lisp just ain't as *READABLE* as Python.

Readability is in the eye of the beholder, but this is not the place to 
argue this.

But this topic does bring up a legitimate question: I have a bunch of 
code that generates HTML using PRINT statements.  I need to convert all 
this code to return strings rather than actually printing them (so I can 
use the results to populate templates).  In Lisp I could do this:

(with-output-to-string (s)
  (let ( (*standard-output* s) )

Is there an equivalent Python trick to capture a function call's output 
as a string?


More information about the Python-list mailing list