Python 有两个小彩蛋!
一、Hello world!
Python 有个内置库,名曰 __hello__
>>> import __hello__
>>> __hello__.main()
Hello world!
所以,以后你做 Hello world!(不是 Hello World 或者 Hello, world! 之类的) 的时候,记得写:
__import__('hello').main()
此外,__hello__
还有亿些东西,源代码长这样(我加了点注释):
initialized = True
class TestFrozenUtf8_1:
"""\u00b6""" # ¶
class TestFrozenUtf8_2:
"""\u03c0""" # π
class TestFrozenUtf8_4:
"""\U0001f600""" # 😀
def main():
print("Hello world!")
if __name__ == '__main__':
main()
二、一篇短文
this
库里长这样:
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print("".join([d.get(c, c) for c in s]))
就是把 s 解密并输出:
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=蟒蛇 禅=馋,所以是蟒蛇的烹饪技巧)
Python 的禅宗,蒂姆·彼得斯 (Tim Peters)
美丽总比丑陋好。
显式比隐式好。
简单胜于复杂。
复杂总比复杂好。
扁平比嵌套好。
稀疏胜于密集。
可读性很重要。
特殊情况并不足以打破规则。
虽然实用性胜过纯洁。
错误永远不应该无声无息地过去。
除非明确沉默。
面对模棱两可,拒绝猜测的诱惑。
应该有一种——最好只有一种——显而易见的方法。
尽管除非您是荷兰人,否则这种方式一开始可能并不明显。
现在总比没有好。
虽然从来没有比现在更好。
如果实现很难解释,这是一个坏主意。
如果实现易于解释,这可能是一个好主意。
命名空间是一个响亮的好主意 -- 让我们做更多这样的事吧!