├── README.md └── cute-wabbit.jpg /README.md: -------------------------------------------------------------------------------- 1 | # 蛇蛋 2 | 3 | Python 中隐藏的彩蛋和笑话列表。 4 | 5 | > Python 比你想象中的更有趣。 6 | 7 | ![Cute Wabbit](./cute-wabbit.jpg) 8 | 9 | A little girl goes into a pet shop and asks for a wabbit. The shop keeper looks down at her, smiles and says: 10 | 11 | "Would you like a lovely fluffy little white rabbit, or a cutesy wootesly little brown rabbit?" 12 | 13 | "Actually", says the little girl, "I don't think my python would notice." 14 | 15 | — Nick Leaton, Wed, 04 Dec 1996, [Python Humor](https://www.python.org/doc/humor/) 16 | 17 | 18 | ### 1. Hello World 19 | 20 | ```py 21 | >>> import __hello__ 22 | Hello World! 23 | ``` 24 | 25 | > Note:在 Python 中打印「Hello, World!」最简单的方式。 26 | 27 | ### 2. Python 之禅 28 | 29 | ``` 30 | >>> import this 31 | 32 | The Zen of Python, by Tim Peters 33 | 34 | Beautiful is better than ugly. 35 | Explicit is better than implicit. 36 | Simple is better than complex. 37 | Complex is better than complicated. 38 | Flat is better than nested. 39 | Sparse is better than dense. 40 | Readability counts. 41 | Special cases aren't special enough to break the rules. 42 | Although practicality beats purity. 43 | Errors should never pass silently. 44 | Unless explicitly silenced. 45 | In the face of ambiguity, refuse the temptation to guess. 46 | There should be one-- and preferably only one --obvious way to do it. 47 | Although that way may not be obvious at first unless you're Dutch. 48 | Now is better than never. 49 | Although never is often better than *right* now. 50 | If the implementation is hard to explain, it's a bad idea. 51 | If the implementation is easy to explain, it may be a good idea. 52 | Namespaces are one honking great idea -- let's do more of those! 53 | ``` 54 | 55 | > Note:Python 设计哲学 & Python 代码风格指南。 56 | 57 | ### 3. 《Python 之禅》丢失的一行 58 | 59 | 《Python 之禅》在 [PEP 20](https://www.python.org/dev/peps/pep-0020/#id2) 中引入,它本应该是 20 条格言,但是最终只写了 19 条。 60 | 61 | Note:也许只是为了说明在文件的结尾总是应该保留一个空行。 62 | 63 | ### 4. 一堂简单的人生课 64 | 65 | ```py 66 | >>> import this 67 | ... 68 | >>> love = this 69 | >>> this is love 70 | True 71 | >>> love is True 72 | False 73 | >>> love is False 74 | False 75 | >>> love is not True or False 76 | True 77 | >>> love is not True or False; love is love # FML 78 | True 79 | ``` 80 | 81 | > Note:不是彩蛋,只是一个解释器里的玩笑。 82 | 83 | ### 5. 反重力漫画 84 | 85 | ```py 86 | >>> import antigravity 87 | ``` 88 | 89 | > Note:这会打开这个 [xkcd 漫画](https://xkcd.com/353),漫画展示了 Python 有多么简单易学。 90 | 91 | ### 6. 这不是一个选择,它定义了我们是谁 92 | 93 | ```py 94 | >>> from __future__ import braces 95 | File "", line 1 96 | SyntaxError: not a chance 97 | ``` 98 | 99 | > Note:这用来快速结束任何关于在 Python 中引入大括号的讨论——不可能! 100 | 101 | ### 7. 起源 102 | 103 | 「Python」这个名字和蛇的种类没有关系。 104 | 105 | > Note:实际的来源是 Guido van Rossum 喜欢的电视剧《[Monty Python's Flying Circus](https://en.wikipedia.org/wiki/Monty_Python%27s_Flying_Circus)》。 106 | 107 | ### 8. 反差 108 | 109 | 下面是打印出《Python 之禅》的 `this.py` 模块的内容: 110 | 111 | ```py 112 | s = """Gur Mra bs Clguba, ol Gvz Crgref 113 | 114 | Ornhgvshy vf orggre guna htyl. 115 | Rkcyvpvg vf orggre guna vzcyvpvg. 116 | Fvzcyr vf orggre guna pbzcyrk. 117 | Pbzcyrk vf orggre guna pbzcyvpngrq. 118 | Syng vf orggre guna arfgrq. 119 | Fcnefr vf orggre guna qrafr. 120 | Ernqnovyvgl pbhagf. 121 | Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf. 122 | Nygubhtu cenpgvpnyvgl orngf chevgl. 123 | Reebef fubhyq arire cnff fvyragyl. 124 | Hayrff rkcyvpvgyl fvyraprq. 125 | Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff. 126 | Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg. 127 | Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu. 128 | Abj vf orggre guna arire. 129 | Nygubhtu arire vf bsgra orggre guna *evtug* abj. 130 | Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn. 131 | Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn. 132 | Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!""" 133 | 134 | d = {} 135 | for c in (65, 97): 136 | for i in range(26): 137 | d[chr(i+c)] = chr((i+13) % 26 + c) 138 | 139 | print("".join([d.get(c, c) for c in s])) 140 | ``` 141 | 生成《Python 之禅》的代码本身违背了自己宣扬的风格建议。它丑陋而不美丽,隐式而不直观。 142 | 143 | > Note:使用了一种叫 [ROT13](https://en.wikipedia.org/wiki/ROT13) 的置换加密法。 144 | 145 | ### 9. 有没有 C/C++ 用户? 146 | 147 | 再次引用《Python 之禅》: 148 | ``` 149 | There should be one-- and preferably only one --obvious way to do it. 150 | ``` 151 | 152 | > Note:在很多语言里常常有两种方法做同一件事,即 `--no` 和 `no--`。这一行包含了一个隐藏的示例。 153 | 154 | ### 10. 命名标识符可以非常酷 155 | 156 | ```py 157 | >>> from math import pi 158 | >>> π = pi 159 | >>> area = π * r**2 160 | 161 | >>> résumé = 'knows Python' 162 | >>> 'Python' in résumé 163 | True 164 | 165 | >>> 我的英文名 = 'Grey Li' 166 | >>> 我的英文名 167 | 'Grey Li' 168 | 169 | >>> import webbrowser 170 | >>> 我的网站 = 'http://greyli.com' 171 | >>> webbrowser.open(我的网站) 172 | ``` 173 | 174 | > Note:Python 3 中支持使用 Unicode 字符作为变量名。尽管如此,使用非英文字符作为变量名可能不是一个好主意,不过它确实会让处理科学公式的工作变得更有意思。 175 | 176 | ### 11. 选一个见面地点 177 | 178 | ```py 179 | >>> from antigravity import geohash 180 | >>> # Your location, a date and that date's (or most recent) DJIA opening. 181 | >>> # 你的位置,一个日期和这个日期(或最近)对应的道琼斯工业指数。 182 | >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68') 183 | 37.857713 -122.544543 184 | ``` 185 | 186 | 这可以用来生成一个 GPS 定位,在基于你所在位置的一个 1 经度长和 1 纬度宽的区域里。 187 | 188 | > Note:这个函数的源码在 [这里](https://github.com/python/cpython/blob/master/Lib/antigravity.py),工作原理解释可以在这个 [xkcd 漫画](https://xkcd.com/426/)看到,也许这就是为什么这个函数也放到了 `antigravity` 模块里。 189 | 190 | ### 12. The FLUFL - Friendly Language Uncle For Life from [PEP 401 -- BDFL Retirement](https://www.python.org/dev/peps/pep-0401) 191 | 192 | ```py 193 | >>> from __future__ import barry_as_FLUFL 194 | >>> 1 <> 2 195 | True 196 | >>> 1 != 2 197 | File "", line 1 198 | 1 != 2 199 | ^ 200 | SyntaxError: invalid syntax 201 | ``` 202 | 203 | 在认识到 Python 3.0 的不等运算符(`!=`)非常糟糕,是手指疼痛导致的错误后,FLUFL(指 Uncle Barry)重新恢复钻石型操作符(`<>`)作为唯一的不等运算符拼写。 204 | 205 | > Note:[PEP 401](https://www.python.org/dev/peps/pep-0401/) 是一个愚人节玩笑(从编号可以看出来)。这个 PEP 声明 Guido van Rossum 要退休了。他会获得一个新的头衔,叫做「BDEVIL」(Benevolent Dictator Emeritus Vacationing Indefinitely from the Language,去度无限期语言假期的仁慈退休独裁者),接任者将会是 Barry Warsaw(即 Uncle Barry),Uncle Barry 的官方头衔是「FLUFL」(Friendly Language Uncle For Life,终生友好语言叔叔)。 206 | 207 | ### 13. InPynite? 208 | 209 | ```py 210 | >>> infinity = float('infinity') 211 | >>> hash(infinity) 212 | 314159 213 | >>> hash(float('-inf')) 214 | -314159 215 | ``` 216 | 217 | 一个散列值是一个固定尺寸的整型,它会标识一个特定的值。仔细观察,你会发现散列值的无限大是 10^5 x π。有意思的是,在 Python3 中,`hash(float('-inf'))` 将会生成 -10^5 x π,而在 Python 2 中则是 -271828(即 10^5 x e)。 218 | 219 | > Note:[来源](https://www.reddit.com/r/Python/comments/6wrd8t/nice_lil_easter_egg_i_suppose/)。 220 | 221 | ### 14. types.CodeType - Not for the faint of heart 222 | 223 | 如果你开始钻进 Python 的内部,你会在 `help` 输出看到一个关于 `types.CodeType` 的警告:不适合心脏脆弱者。 224 | 225 | ```py 226 | >>> import types 227 | >>> help(types.CodeType) 228 | ... 229 | Help on class code in module builtins: 230 | 231 | class code(object) 232 | | code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring, 233 | | constants, names, varnames, filename, name, firstlineno, 234 | | lnotab[, freevars[, cellvars]]) 235 | | 236 | | Create a code object. Not for the faint of heart. 237 | | 238 | | Methods defined here: 239 | | 240 | 241 | ... 242 | ``` 243 | 244 | ## 添加更多 245 | 246 | 欢迎创建 PR 来添加更多关于 Python 的彩蛋和玩笑。 247 | 248 | ## License 249 | 250 | 原始版本翻译自 https://github.com/OrkoHunter/python-easter-eggs 项目,原项目没有许可协议,已通过邮件申请获得翻译和发布许可。禁止商业转载。 -------------------------------------------------------------------------------- /cute-wabbit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/greyli/shedan/7d59551a692acb94bcb8d130523780958bf29d43/cute-wabbit.jpg --------------------------------------------------------------------------------