Making the Zen of Python more useful

Peter Hansen peter at engcorp.com
Fri Apr 2 09:30:31 EST 2004


Andrew Henshaw wrote:
> Yesterday, I was writing some test code for a module and I wanted some
> line-oriented text to use as a data source.  I thought about using a
> docstring from one of the standard modules; but, it occurred to me that the
> Zen of Python text from the 'this' module would be *much* better, as it is
> more readable and would be a nice thing for our students to see.
> 
> Unfortunately, the actual text is difficult to use:
>   o the text is ROT-13 encoded and the module's translation routine is
> inline
>   o the text is printed to stdout on import.  I understand why Tim did this,
> but it did interfere with my purpose.
> 
> So, in order to use the module for my test code, I had to implement a short
> ROT-13 translator, import the sys module, redirect stdout to a simple
> object that provided a null write method, import this, restore stdout, and
> then rot13(this.s).

I'm not sure why you had to do quite all that.  The following is 
sufficient, and you don't need a custom ROT-13 thingie:

 >>> import StringIO, sys
 >>> s = StringIO.StringIO()
 >>> sys.stdout = s
 >>> import this
 >>> sys.stdout = sys.__stdout__
 >>> s.getvalue()
"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter



More information about the Python-list mailing list