如果你刚接触 Python,一定见过或即将见到这样一行神秘的代码:import this。它不像 import os 或 import sys 那样提供具体的功能模块,而是会输出一段看似"鸡汤"的文字。这段文字就是 《The Zen of Python》(Python 之禅),由 Python 核心开发者 Tim Peters 撰写,被 Guido van Rossum 钦点为 Python 社区的非官方设计哲学指南。
眼看会议日期逼近,Tim Peters 和 Barry Warsaw 决定采用”淘汰赛”机制:两人轮流评审,每次淘汰一半留下一半。经过几轮残酷的筛选,最终 “import this” 脱颖而出。
大家对这个结果非常满意,但随即意识到一个尴尬的问题:他们必须真的实现这个功能。经过简短讨论,”import this” 被定为输出 Tim Peters 写的《The Zen of Python》。为了让这个彩蛋更有趣,他们决定用 ROT13 加密算法对文本做简单混淆——这就是你今天在源码里看到的那串乱码的由来。
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!