Tuesday, June 8, 2010

import this and The Zen of Python

Richard Jones is working on a talk for PyCon Australia and asked me about the history of the Zen of Python, Tim Peters' eternal words of wisdom, often quoted, on the essential truths of Python. At first, I couldn't find a reference to the first publication of this list, but then I did a better search of my archives and found that it was originally sent to the python-list mailing list on June 4, 1999, under the subject "The Python Way".

Interestingly enough, because I couldn't find that first reference immediately, I went back into my archives and researched the "this" module. Did you know that if you type the following at a modern Python interpreter, you get the Zen of Python?

% python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
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!

The story behind "import this" is kind of funny, and occurred totally behind the scenes, so I thought it might be interesting to relate how it happened. Maybe something to add to Guido's History of Python blog.

Anyway, back in fall 2001, Foretec was organizing the International Python Conference #10 (IPC 10, precursor to Pycon). Foretec was a conference organizing company owned by CNRI, which earlier had employed Guido, Fred, Jeremy and myself, and which by 2000 we had left to form Pythonlabs at various friendly (and occasionally disfunctional) corporate homes. By September 13, 2001 (yes, how weird!) we were working for our friends at Zope Corporation (perhaps not yet rebranded from Digitial Creations), but Foretec was still organizing the IPCs. They wanted to have a slogan for the conference, which could be printed on a t-shirt, and wanted to gather submissions from the Python community. Pythonlabs agreed to judge the entries and select a winner. However, I think Guido's wife was due any day and he didn't have much time or energy to go through the entries.

We got something like 500 entries, almost all of them terrible. Tim's exact words were "Jeez Louise, I can't look at these for more than 5 minutes w/o my brain turning to mush" and yet he still managed to do an initial cut down to something like 130 entries. While we had agreed to choose a winner, we procrastinated until the last minute and by then it was obvious that Tim and I were the only ones crazy enough to still care. Tim suggested that we trade the list back and forth between the two of us, each cutting the list in half until there was just one left. Tim is much better at math than me, and I had forgotten about Python's integer division so I was left to choose from between the last two entries: "Bite off all you like - Chewing is optional" (yes, I said most were terrible ;), and "import this". At the last minute, Tim resurrected a fun one that we had both noticed approvingly early on: "Let's we study about Python program".

While that last one had lots of appeal, I liked the irreverent, almost sneering tone of "import this". I saw the potential for a great, Michael Jackson-esque t-shirt.

As soon as we'd chosen "import this" I realized we just had to implement it. Python 2.2 was about to be released and I proposed that we turn off checkin notifications and sneak in a "this.py" module which when imported just printed the Zen of Python. Tim or Guido suggested further that we rot13 the contents of the module just for a little extra obfuscation, and we told no one outside our little group. According to my intergoogles spelunking, as soon as IPC 10 was concluded, we commemorated the event by committing this.py to what was to become Python 2.2.1, thus adding to the affront of new features in point releases. IIRC, it took a long time for someone to find our little easter egg.

That was all back in the day when the Python community had a sense of humor.

29 comments:

  1. Wow, thanks for enlightening us.

    ReplyDelete
  2. "That was all back in the day when the Python community had a sense of humor."

    I pine for cheeseshop... :/

    ReplyDelete
  3. Does anyone claim to be the first person to have discovered the functionality of this.py?

    ReplyDelete
  4. The day we were pressured to rename the cheeseshop was a very sad day indeed.

    ReplyDelete
  5. I like the last hyperlink to your post.. hahaha :)

    ReplyDelete
  6. I didn't believe it until I tried 'import this' from my terminal.

    ReplyDelete
  7. Very nice post. For us Python beginners this is an interesting read.

    Helen Neely

    ReplyDelete
  8. Thanks for the history. I do believe Alex and I are officially the first couple to use the zen of python as a reading in our wedding.

    ReplyDelete
  9. I think the Python Secret Underground has been very successful in its campaign to eliminate humor in the Python community. They are doing this to cause a great cosmological mashup in 2012, which is made possible because of Google's overuse of Guido's time machine. The problems could apparently be easily prevented by a simple application of [NO CARRIER]

    ReplyDelete
  10. A quick link to the this.py is a nice addition for those that are interested in the implementation.

    http://code.python.org/hg/trunk/file/e24b18715bfe/Lib/this.py

    ReplyDelete
  11. Of course, I love the Cheeseshop name too (and in fact still use it whenever I can). I might have even come up with it, but that could just be my bloated ego and/or age affected faulty memory speaking. :)

    @Anna: wow, that is really cool! You guys are geeks :)

    Oh, and the PSU does NOT exist.

    ReplyDelete
  12. @Anna I thought we were nerdy for having readings from Calvin & Hobbes and Winnie the Pooh, but incorporating the Zen is epic. Well done! :-)

    ReplyDelete
  13. reminds of the time i went through german customs wearing my 'import this' ipc t-shirt.. a few extra questions where asked :-) still a classic though. I dig the python threads snake 'zen of python' t-shirt as a modern remake.

    ReplyDelete
  14. This posting is now available in Japanese, thanks to SHIBUKAWA Yoshiki

    ReplyDelete
  15. http://python-history-jp.blogspot.com/2010/07/import-thisthe-zen-of-python2.html

    ReplyDelete
  16. Was the "Tim" who was deciding between the slogans the same "Tim" who wrote the chosen version? Was the slogan renamed from "import this" to "the zen of python"?

    ReplyDelete
  17. @Sawyer: Well, the "Tim Peters" that we all think we know and love is reportedly a hobo handsomely paid (and cleaned up handsomely) by the real "Tim Peters" who continues to live a reclusive life and that no one except Guido has actually met. I used to think there was an entire "Tim Peters" clone army out there, answering an astounding number of emails and doing some amazing things with, to, and for Python. Sadly, all the hobo Tims seemed to have retired, and there's only the one left that occasionally requests Kung Pao Chicken and Walls of Chocolate. Once this obviously fake "Tim Peters" stopped consuming bags of ham, it was all downhill. So who really knows if they were the same "Tim Peters" after all?

    ReplyDelete
  18. I just found that the module "this" has some attributes to play with.
    >>> import this
    >>> print ''.join(this.d[c] if c in this.d else c for c in this.s)

    ReplyDelete
  19. Cleaner version
    >>> print ''.join(this.d.get(c, c) for c in this.s)

    ReplyDelete
  20. Can I use the spam1 picture in my Pycon 2012 keynote? To reference that people stay in the community because of friends ... Thanks!

    ReplyDelete
  21. @Stormy: which picture? (link?) But I'm sure it will be fine. :)

    ReplyDelete
  22. @Barry, http://www.python.org/workshops/2002-02/spam1folks.jpg

    ReplyDelete
  23. @Stormy: I honestly don't have any idea who took that picture, but go for it!

    ReplyDelete
  24. I'm impressed that you knew about the walls of chocolate back in 2010. The end of an ear occurred recently when Tim left the Reston area. Their loss, his gain, I firmly believe. A great man.

    ReplyDelete
  25. Barry, the link in your post seems to be changed (the one about Tim's original mail).

    Searched for it, I think this is the one:

    http://mail.python.org/pipermail/python-list/1999-June/001951.html

    Regards,

    ReplyDelete
  26. Thanks Facundo! I bet the archive got regenerated at some point, which breaks all the links (Pipermail gives me a sad). I've updated the link in the original article.

    ReplyDelete