イースターエッグ

この記事は Python Tips Advent Calendar 2012 25日目の記事です。


最後は、クリスマスとキリストで繋がった、イースターエッグについてです。
Python に隠された卵を探して楽しみましょう。

The Zen of Python

import this をすることで The Zen of Python がいつでも表示できます。

>>> import 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!

しかしこの項目の多さ、複雑だし、詰め込みすぎだし、読み易くないしで、The Zen of Python のテキストは The Zen of Python を意識せずに書かれている気がする*1

空白でのインデントをやめる

from __future__ import braces をすることで、空白でのインデント制御を C のようなブレースのスタイルにできるらしいです。
ということでやってみると…。

>>> from __future__ import braces #doctest: +ELLIPSIS
Traceback (most recent call last):
    ...
SyntaxError: not a chance ...

not a chance(絶対にあり得ないよ!)というエラーが出ます。
ブレースによる制御が標準に入ることは無さそうです*2

import __hello__

>>> import __hello__
Hello world...

なぜか Hello world... という文字列が出力されます。
あと __phello__ を使っても同じです。

>>> import __phello__
Hello world...
>>> from __phello__ import spam
Hello world...

これは C 言語などから Python のモジュールを import するときに、テストのために使ったりするようです。

antigravity

>>> import antigravity

と書くと、ブラウザが開き、このページに飛ばされます。
こんな感じのことを言ってるはず。

A「Python!」
B「飛んでる!どうやって!?」
A「昨日の夜、Python を勉強したんだ。とてもシンプルだったよ。
  Hello World を出力するなら単に print "Hello, World!" と書くだけでいいんだ」
B「動的型付け?空白インデント?よく分からないよ…。」
A「こっちにおいでよ!プログラミングがまた楽しくなるよ!ここからは世界全体が見渡せるよ!」
B「でも君はどうやって飛んだんだい?」
A「import antigravity って打っただけだよ」
B「それだけ?」
A「……あと、Python と比較するために薬棚の中身を全部飲んでみたけど……。
  でもこれは Python だよ!多分!」


.

*1:Python コードじゃないからいいのかもしれないけど

*2:詳細は (訂正)Pythonの__future__ - 西尾泰和のはてなダイアリー を参照