Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of import this

Tags:

python

module

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?

like image 716
caird coinheringaahing Avatar asked Apr 23 '17 22:04

caird coinheringaahing


People also ask

What does import this do in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.

What is the purpose of the import command?

The import command is used to call the built-in and user-defined modules in Python files.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.

What are the three types of import statement in Python?

There are generally three groups: standard library imports (Python's built-in modules) related third party imports (modules that are installed and do not belong to the current application) local application imports (modules that belong to the current application)


1 Answers

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.

like image 113
E. Ducateme Avatar answered Oct 17 '22 06:10

E. Ducateme