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

Ron Garret rNOSPAMon at flownet.com
Sun Jan 2 21:15:46 CET 2005


In article <cr9ebu$re1$1 at panix3.panix.com>, aahz at pythoncraft.com (Aahz) 
wrote:

> In article <roy-D9F00D.12570402012005 at reader1.panix.com>,
> Roy Smith  <roy at panix.com> wrote:
> >In article <rNOSPAMon-35B48E.09261402012005 at news.gha.chartermi.net>,
> > Ron Garret <rNOSPAMon at flownet.com> wrote:
> >> In article <1104657461.868175.252380 at c13g2000cwb.googlegroups.com>,
> >>  "Erik  Bethke" <erikbethke at gmail.com> 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) )
    (call-html-generating-code)
    s))

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

Thanks,
rg



More information about the Python-list mailing list