12

Typing

import this 

will return the Zen of Python, but nowhere do I seem to be able find a solution about how to set it equal to a string variable which I can use further on in my code...

Pavel
  • 7,436
  • 2
  • 29
  • 42
MaxQ
  • 595
  • 1
  • 8
  • 25
  • 3
    `this` is a module. Look at the source code of that module to see what it contains. That should get you started. – BrenBarn May 21 '14 at 21:44
  • `this` defines 4 names (`c`, `d`, `i` and `s`) - you can actually get the text using only two of them – jonrsharpe May 21 '14 at 21:47

5 Answers5

13

You can temporarily redirect stdout to a StringIO instance, import this, and then get its value.

>>> import sys, cStringIO
>>> zen = cStringIO.StringIO()
>>> old_stdout = sys.stdout
>>> sys.stdout = zen
>>> import this
>>> sys.stdout = old_stdout
>>> print zen.getvalue()
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!

This code works on python2.7 -- for python 3, use io.StringIO instead of cStringIO.StringIO, and also have a look at contextlib.redirect_stdout which was added in 3.4. That would look like this:

>>> import contextlib, io
>>> zen = io.StringIO()
>>> with contextlib.redirect_stdout(zen):
...    import this
...
>>> print(zen.getvalue())

In Python3.8+ you can also do:

>>> import contextlib, io
>>> with contextlib.redirect_stdout(zen := io.StringIO()):
...    import this
...
>>> print(zen.getvalue())
Jonathan1609
  • 1,809
  • 1
  • 5
  • 22
Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97
  • 2
    I definitely think this is the most appropriate approach for answering the question. It's generalizable to other modules that happen to have printed output and it's resistant to implementation changes to the `this` module. The other answers, while they work, are so specific that we might as well tell the OP to copy the text themselves and paste it into a string literal, or link to the on-line version of [PEP 20](http://legacy.python.org/dev/peps/pep-0020/), etc. If the OP is trying to understand how it works, then what they really want is this: http://stackoverflow.com/questions/5855758 – John Y May 21 '14 at 22:10
4

Let's look at what this.py does:

s = "some encrypted string"
d = a map to decrypt the string

print "".join([d.get(c, c) for c in s])

Let's note that the encryption is just ROT13.

So if we really wanted to grab the string, we could do:

import this
s = this.s.decode('rot13')

Or, to explicitly follow the style of the this.py module...

import this
s = "".join([this.d.get(c, c) for c in this.s])
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1

I think the accepted answer is overcomplicated for this case (while very interesting for capturing the standard output in general).

In Python 3, you can get the Zen of Python as a string by simply doing:

import this
import codecs

zen_of_python = codecs.encode(this.s, 'rot13')
Marc Garcia
  • 3,287
  • 2
  • 28
  • 37
0

the string is stored in this.s, but that's funny, because it's encrypted:

>>> help(this)
NAME
    this

FILE
    /usr/lib/python2.7/this.py

MODULE DOCS
    http://docs.python.org/library/this

DATA
    c = '!'
    d = {'A': 'N', 'B': 'O', 'C': 'P', 'D': 'Q', 'E': 'R', 'F': 'S', 'G': ...
    i = 25
    s = "Gur Mra bs Clguba, ol Gvz Crgref\n\nOrnhgvshy vf o...bar ubaxvat ...

$ head /usr/lib/python2.7/this.py
s = """Gur Mra bs Clguba, ol Gvz Crgref

Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Pavel
  • 7,436
  • 2
  • 29
  • 42
  • The dict translates the letters to something readable, but there's some weird mojo going on in this module. – Nacho May 21 '14 at 21:52
  • 2
    The letters are shifted alphabetically by some characters: http://stackoverflow.com/questions/5855758/can-anyone-explain-me-the-source-code-of-python-import-this – Joren May 21 '14 at 21:53
  • 2
    Yeah, opened the module up in PyCharm and the magic was gone. It was more fun when I thought there was a sinister plot afoot. – Nacho May 21 '14 at 21:55
-2

I did it in Pycharm (IDE) as below:

import this
print()
mindcoder
  • 377
  • 3
  • 11