导入`this`模块?

时间:2013-05-02 16:36:44

标签: python

在Python shell中键入以下内容不会产生错误:

from this import *

什么是this模块?

1 个答案:

答案 0 :(得分:4)

this是Tim Peters撰写的zen of python

>>> from this import *
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.
.....
>>> d
{'A': 'N', 'C': 'P', 'B': 'O', 'E': 'R', 'D': 'Q', 'G': 'T', 'F': 'S', 'I': 'V', 'H': 'U', 'K': 'X', 'J': 'W', 'M': 'Z', 'L': 'Y', 'O': 'B', 'N': 'A', 'Q': 'D', 'P': 'C', 'S': 'F', 'R': 'E', 'U': 'H', 'T': 'G', 'W': 'J', 'V': 'I', 'Y': 'L', 'X': 'K', 'Z': 'M', 'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r', 'd': 'q', 'g': 't', 'f': 's', 'i': 'v', 'h': 'u', 'k': 'x', 'j': 'w', 'm': 'z', 'l': 'y', 'o': 'b', 'n': 'a', 'q': 'd', 'p': 'c', 's': 'f', 'r': 'e', 'u': 'h', 't': 'g', 'w': 'j', 'v': 'i', 'y': 'l', 'x': 'k', 'z': 'm'}
>>> c
'!'

为什么from this import *会引发错误?

使用上面的语法只需将this模块的名称空间合并到当前名称空间中。

>>> import this
>>> dir(this)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'c', 'd', 'i', 's']

还可以尝试:

import antigravity