Python中的前 5 个复活节彩蛋
Python确实是一种有趣的语言,具有非常好的文档。在本文中,我们将介绍一些有趣的东西,这些东西没有记录在案,因此被认为是Python的复活节彩蛋。
1.你好世界
大多数程序员应该从打印“Hello World!”开始您的编程之旅。你会相信Python有一个秘密模块来打印“Hello World”并且模块的名称是 __hello__
Python3
import __hello__
Python3
# redirects you to https://xkcd.com/353/
import antigravity
Python3
import this
Python3
from __future__ import barry_as_FLUFL
1 <> 2
1 != 2
Python3
from __future__ import braces
输出:
Hello world!
2.反重力
如果您整天都在输入代码感到无聊,请检查Python中的反重力模块,它会将您重定向到
https://xkcd.com/353/ 网络漫画。
Python3
# redirects you to https://xkcd.com/353/
import antigravity
3. Python之禅
《 Python之禅》是Python设计原则的指南。它由 19 条设计原则组成,由美国软件开发人员 Tim Peters 编写。这也是迄今为止唯一一个在Python开发人员指南中被称为“复活节彩蛋”的“官方”复活节彩蛋。您可以通过导入模块“this”来查看它们。
Python3
import this
输出:
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!
4. FLUFL [终生友好的语言叔叔]
认识到Python 3.0 中的 != 不等式运算符是一个可怕的、导致手指疼痛的错误,FLUFL 恢复了 <> 菱形运算符作为唯一的拼写。
Python3
from __future__ import barry_as_FLUFL
1 <> 2
1 != 2
输出:
True
SyntaxError: with Barry as BDFL, use '<>' instead of '!='
5. 大括号
与大多数语言不同, Python使用缩进而不是花括号“{}”。在从 C++ 或Java等语言过渡到Python时,适应缩进有点困难。因此,如果我们尝试在 __future__ 模块中使用大括号, Python会给出一个有趣的回答“没有机会”。
Python3
from __future__ import braces
输出:
File "", line 1
SyntaxError: not a chance