38

There is a well known Easter Egg in Python called import this that when added to your code will automatically output

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!

So what exactly is the purpose of this?

It also contains 4 variables:

  • this.c which contains the number 97
  • this.s which contains The Zen of Python encoded in rot13
  • this.i which contains the number 25
  • this.d which contains the following dictionary: {'G': 'T', 'z': 'm', 'C': 'P', 't': 'g', 'F': 'S', 'p': 'c', 'o': 'b', 'P': 'C', 'V': 'I', 'T': 'G', 'Q': 'D', 'W': 'J', 'R': 'E', 'i': 'v', 'M': 'Z', 'w': 'j', 'O': 'B', 'L': 'Y', 'n': 'a', 'd': 'q', 'D': 'Q', 'K': 'X', 'H': 'U', 'S': 'F', 'N': 'A', 'A': 'N', 'B': 'O', 'v': 'i', 'a': 'n', 'I': 'V', 'J': 'W', 'u': 'h', 'q': 'd', 'j': 'w', 'e': 'r', 'x': 'k', 's': 'f', 'X': 'K', 'E': 'R', 'm': 'z', 'h': 'u', 'g': 't', 'y': 'l', 'U': 'H', 'c': 'p', 'r': 'e', 'f': 's', 'l': 'y', 'b': 'o', 'Y': 'L', 'k': 'x', 'Z': 'M'}

So what actually is the purpose of this (IMO) useless module? Has Guido Van Rossum ever said why he included it?

  • 4
    Possible duplicate of [Attributes of Python module \`this\`](http://stackoverflow.com/questions/37301273/attributes-of-python-module-this), have you checked it? – Taku Apr 23 '17 at 23:10
  • 3
    And this: https://www.python.org/dev/peps/pep-0020/ from pep 20. And this as well: https://hg.python.org/cpython/file/3.5/Lib/this.py – Taku Apr 23 '17 at 23:12

1 Answers1

33

To quote from the Python Enhancement Proposal (PEP) associated with this: https://www.python.org/dev/peps/pep-0020/

"Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down."

Thus, the stated purpose of this module is to spell out guidelines to related to the development of Python code.

Following these guidelines will likely improve the readability, usability and maintainability of Python code. None of which seem "useless".

NOTE 1: the code that produces this list of guidelines purposely breaks most of them. = )

NOTE 2: BDFL stands for Benevolent Dictator for Life and referred to Guido van Rossum, the developer of Python.

If you are interested in learning more about the internal workings of the this module, see this stackoverflow post: Attributes of Python Module This.

E. Ducateme
  • 4,028
  • 2
  • 20
  • 30