Purpose of import this
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.
Related videos on Youtube
caird coinheringaahing
Hi! I'm caird coinheringaahing and I'm an active member of CGCC, where I help clean up low quality posts, as well as post the occasional answer or challenge. I'm also a room owner of The Nineteenth Byte, where I help keep the room clean and tidy I typically go by any traditional pronouns (he/she/they), so you can use any of those you like with me, I won't get offended. If you want to refer to me by name, any of "caird", "Jay" or "Chloe" are fine. If one of my answers doesn't work, don't hesitate to comment so I can update it. Furthermore, if any of my answers are lacking explanations, let me know and I'll add one in. My profile picture is of my adorable puppy, Patch. Over the winter, he swaps his Stack Exchange hat for a Santa hat. You can get in touch with me by: Leaving a comment on one of my answers Pinging me in chat with @cairdcoinheringaahing Pinging me in the PPCG Discord with @cairdcoinheringaahing Why my username is what it is Posts I'm proud of: WW2 meets golfing languages Implement Multiplicative Fuzzy Logic 22 bytes to implement + in Jelly My valuable contributions to CGCC that aren't challenges or answers: Room owner of The Nineteenth Byte Wrote the Welcome to Code Golf and Coding Challenges welcome page Copy Editor and Socratic badges on main Deputy and Inquisitive badges and silver discussion tag badge on meta
Updated on March 18, 2021Comments
-
caird coinheringaahing about 3 years
There is a well known Easter Egg in Python called
import this
that when added to your code will automatically outputThe 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 number97
this.s
which containsThe Zen of Python
encoded inrot13
this.i
which contains the number25
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?
-
Taku about 7 yearsPossible duplicate of Attributes of Python module `this`, have you checked it?
-
Taku about 7 yearsAnd this: python.org/dev/peps/pep-0020 from pep 20. And this as well: hg.python.org/cpython/file/3.5/Lib/this.py