Tip 2 Import This

Pythonic Programming — by Dmitry Zinoviev (10 / 116)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Hello, World! | TOC | Know Ownership and Licensing 👉

★2.7, 3.4+ Every Python installation comes with the module named this. The module has nothing to do with the namesake Java or C++ keyword denoting a reference to a class object. The module contains The Zen of Python — a set of guiding principles that define the “Pythonic” programming style. Just like with any other kind of Zen, the Zen of Python is learned by practicing. Practicing meditation does not hurt, but practicing good programming helps. Let’s start our practice by looking at the principles first. Type:

​ ​import​ ​this​

And you will see The Zen of Python.

I will occasionally refer to some of these principles throughout the rest of the book.

For a reason beyond my understanding (probably Zen-related), The Zen of Python is stored in the module as a scrambled string, this.s (remember, this is the name of the module), that comes with a dictionary, this.d, for unscrambling. The unscrambling process is trivial:

​ ​''​.join(this.d.get(x, x) ​for​ x ​in​ this.s)

I do not know why the authors of the module decided to scramble the contents. Quite possibly nobody knows, aside from them.

👈 Hello, World! | TOC | Know Ownership and Licensing 👉

Pythonic Programming by Dmitry Zinoviev can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.