[Baypiggies] history of import this

Kelly Yancey kelly at nttmcl.com
Fri Jun 11 03:30:58 CEST 2010

On 6/10/2010 4:59 PM, Langton, Asher wrote:
> On 6/10/10 4:53 PM, "akleider at sonic.net"<akleider at sonic.net>  wrote:
>> This is pretty cool.
>> What syntax would one use to capture the string that gets printed when one
>>>>> import this
>> ????
> Try this:
>>>> import this
>>>> s = "".join([this.d.get(c, c) for c in this.s])
> To where this comes from, take a look at this.py in the Python source code.

The real trick is capturing the string that gets printed by "import 
this" without actually printing it to stdout when you import this.

The shortest, albeit somewhat hackish, way I can think of is to use the 
hint that Aahz dropped earlier in the thread:

 >>> import subprocess
 >>> wisdom = subprocess.Popen(['python', '-m', 'this'],
...                           stdout=subprocess.PIPE).communicate()[0]
 >>> print wisdom
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!


More information about the Baypiggies mailing list