"import this, that, other, stuff"是做什么用的?

5
#!/usr/bin/env python
import this, that, other, stuff
class SomeObject(object):
    pass

def some_function(*args,**kwargs):
    pass

if __name__ == '__main__':
    print( "This only executes when %s is executed rather than imported" % __file__)

上述代码是做什么的呢? 我得到了以下输出。
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!

我是Python的新手,但非常好奇想要了解。请帮帮我。


4
这是Python中的一个隐藏彩蛋,如果你执行import this,你会得到这个文本。 - RemcoGerlich
1
如果你实际上没有其他名为this.py、that.py、other.py和stuff.py的模块,那么看起来你只是复制了不起作用的示例代码。它不应该完全像这样工作。 - RemcoGerlich
4个回答

5

请尝试以下操作:

import this

这一行代码将会让解释器输出Python之禅


1
import this

本文介绍了由Tim Peters所著的Python之禅。由于它是一种导入方式,无论您的文件是直接执行还是通过导入执行,您的if __name__ == '__main__':块中的print()调用都是不正确的。除非您在计算机上有that.pyother.pystuff.py这些模块,否则您的代码也会抛出错误,但无论您是直接执行还是导入执行,结果都将是相同的。那些模块(thatotherstuff)并不是彩蛋 :)


0

尽管这是一首诗,但它实际上是一个编码在this.py中的打印函数,代码如下:

$ touch test.py
$ echo "print('Zen of Python')" > test.py
$ python -c "import test"
Zen of Python

0

如果在Python解释器中执行以下操作:1)help() 2)modules - 您将看到模块列表,“this”是其中之一;只需键入“this”(不带引号)- 它包含Python的ZEN,这就是您在此处输出的内容;仅导入this将给您相同的输出。但这是一个检查Python模块中有什么的地方,输入任何列出的模块名称并查看。该代码只是一个简单的示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接