Making the Zen of Python more useful

Joe Mason joe at notcharles.ca
Fri Apr 2 10:55:52 EST 2004


In article <c4k1hb$cdg$1 at news-int.gatech.edu>, Andrew Henshaw wrote:
> In article <-5ydnTEmxfuZHvDdRVn-iQ at powergate.ca>, peter at engcorp.com says...
>>
>>Andrew Henshaw wrote:
>>
>>> In article <BPWdnVtfl7ma5PDdRVn-sw at powergate.ca>, peter at engcorp.com says...
>>>>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 ...
>>> 
>>> Much better.  Still pretty awkward for the purpose I described.  Perhaps, 
> the 
>>> best solution, overall.
>>
>>Okay then, how about this one?  :-)
>>
>> >>> import this
>>The Zen of Python, by Tim Peters\n\nBeautiful is ...
>> >>> this.s.decode('rot-13')
>>u"The Zen of Python, by Tim Peters\n\nBeautiful is ...
>>
>>-Peter
> 
> Hey, that's very slick!  But ... (starting to feel like a whiner), my main 
> problem was with the automatic printing of the text upon import.  Obviously, 
> that functionality needs to remain.  It would just be nice if there was 
> some clean way of (sometimes) using the module without it.

Personally, I'd just make a "zen" module which imports "this" after
redirecting stdout as described above, and give that to your students.

Joe



More information about the Python-list mailing list