什么是"导入这个,那个,其他的东西"在做什么?

时间:2016-04-12 10:35:37

标签: python

#!/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 个答案:

答案 0 :(得分:5)

尝试执行以下操作:

import this

这一行将使解释器输出 Python的禅“

答案 1 :(得分:1)

import this

给你Tim Peters的Zen of Python。由于它是导入的,无论文件的执行方式如何(直接导入或导入),if __name__ == '__main__':阻止print()阻止其that.py阻止。除非您的计算机上有other.pystuff.pythat,否则您的代码也会出错,但无论您是直接执行还是导入,结果都是一样的。这些模块(otherstuffdisable_functions = "mail, sendmail, mailx")不是复活节彩蛋:)

答案 2 :(得分:0)

如果在python解释器中您执行以下操作:1)help()2)模块 - 您看到一个模块列表,“this”是列表中的一个;只需输入“this”(不带引号) - 它包含Python的ZEN,这是你在输出中获得的;单独导入这将给你相同的输出。但它是一个检查python模块中的内容的地方,键入任何列出的模块名称并查看。代码只是一个熊的例子。

答案 3 :(得分:0)

尽管有这首诗,它还是以this.py编码的打印函数:

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