Can Python Make You fly?
Well, turns out Python can actually make you fly!
Haven't tried it before? Don't worry, I'll guide you. Like other things in Python, it's super simple. Here's what you have to do:
import antigravity
What was that?
That was an easter egg. import antigravity
opens up a web browser that points to the classic XKCD comic mentioning Python. You know what, the developer's didn't stop here either, there's another Easter egg inside the Easter egg.
If you look at the code, there's a function defined that purports to implement the XKCD's geohashing algorithm.
Is there more stuff like this?
Yes! I know a few of them. Let me walk through some more Easter eggs or hidden gems I found on the internet:
Python understands that love is complicated
import this
Wait, what's this?
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!
It's the Zen of Python!
>>> love = this
>>> this is love
True
>>> love is True
False
>>> love is False
False
>>> love is not True or False
True
>>> love is not True or False; love is love # Love is complicated
True
this
module in Python is an easter egg for The Zen Of Python (PEP 20). And if you think that's already interesting enough, check out the implementation of this.py.
Interestingly, the code for the Zen violates itself (and that's probably the only place where this happens). Regarding the statement love is not True or False; love is love
, it's ironic but self-explanatory.
Brace yourself!
If you are one of the people who don't like using whitespace in Python to denote scopes, you can use the C-style {} by importing,
from __future__ import braces
Here's what we get after the above import:
File "some_file.py", line 1
from __future__ import braces
SyntaxError: not a chance
The __future__
module is normally used to provide features from future versions of Python. The "future" here is, however, ironic. This is an Easter egg concerned with the community's feelings on this issue.
How simple can "Hello world" program be?
It's this simple:
>>> import __hello__
Hello World!
Let's meet Friendly Language Uncle For Life
>>> from __future__ import barry_as_FLUFL
>>> "Ruby" != "Python" # there's no doubt about it
File "some_file.py", line 1
"Ruby" != "Python"
^
SyntaxError: invalid syntax
>>> "Ruby" <> "Python"
True
There we go.
This is relevant to PEP-401, released on April 1, 2009 (now you know what it means). Quoting from the PEP,
it was recognized that the != inequality operator in Python 3.0 was a horrible, finger-pain inducing mistake.
The FLUFL reinstates the <> diamond operator as the sole spelling.
There's more that Uncle Barry had to share in the PEP, which you can read here.
Inpinity
The spelling is intended.
>>> infinity = float('infinity')
>>> hash(infinity)
314159
>>> hash(float('-inf'))
-314159
Hash of infinity in Python is 10⁵ x π. Interestingly, hash of float('-inf')
is "-10⁵ x π" in Python 3, whereas it's "-10⁵ x e" in Python 2.
Need more?
I've been digging around such amazing Python gems for quite a few months now. I'd recommend you check out What the f*ck Python?, a curated collection of such subtle and tricky snippets in Python.