好玩的Python彩蛋
Python作為一門嚴(yán)謹(jǐn)?shù)木幊陶Z言,也充滿了各種娛樂、惡搞、實(shí)用的彩蛋,程序員真的是一群創(chuàng)意無極限的家伙。筆者總結(jié)了Python中幾個(gè)比較有趣的彩蛋。
1. Hello World
相傳古時(shí)候有個(gè)退休的程序員,在家閑來無事,決定修習(xí)書法之道。第一日備好筆墨紙硯,便揮毫落紙寫下一行大字:“hello, world”。hello world 是程序員學(xué)習(xí)一門新的編程語言的開始,而Python干脆把它嵌入到了內(nèi)置模塊中。
>>> import __hello__ Hello World...到了python3,輸出結(jié)果稍微有改動,把...替換成了!
>>> import __hello__ Hello World!2. Python之禪
import this 中隱藏了一首《Python之禪》的詩,它是Python中的『八榮八恥』,作者是 Tim Peters ,每個(gè)有追求的Python程序員都應(yīng)該謹(jǐn)記于心。
>>> import this The Zen of Python, by Tim PetersBeautiful 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!參考翻譯:
Beautiful is better than ugly. 優(yōu)美勝于丑陋。Explicit is better than implicit. 顯式勝于隱式。Simple is better than complex. 簡單勝于復(fù)雜。Complex is better than complicated. 復(fù)雜勝于難懂。Flat is better than nested. 扁平勝于嵌套。Sparse is better than dense. 分散勝于密集。Readability counts. 可讀性應(yīng)當(dāng)被重視。Special cases aren’t special enough to break the rules. Although practicality beats purity. 盡管實(shí)用性會打敗純粹性,特例也不能凌駕于規(guī)則之上。Errors should never pass silently. Unless explicitly silenced. 除非明確地使其沉默,錯(cuò)誤永遠(yuǎn)不應(yīng)該默默地溜走。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 way not be obvious at first unless you’re Dutch. 雖然一開始這種方法并不是顯而易見的,但誰叫你不是Python之父呢。Now is better than never. Although never is often better than right now. 做比不做好,但立馬去做有時(shí)還不如不做。If the implementation is hard to explain, it’s a bad idea. 如果實(shí)現(xiàn)很難說明,那它是個(gè)壞想法。If the implementation is easy to explain, it may be a good idea. 如果實(shí)現(xiàn)容易解釋,那它有可能是個(gè)好想法。Namespaces are one honking great idea – let’s do more of those! 命名空間是個(gè)絕妙的想法,讓我們多多地使用它們吧!3. 反地心引力
import antigravity是一個(gè)搞笑的彩蛋,它直接跳轉(zhuǎn)到一副漫畫網(wǎng)頁http://xkcd.com/353/,漫畫非常有意思,Cueball漂浮在天空中,一個(gè)朋友好奇的問他怎么飛起來的。
Friend:哇!你在飛,怎么做到的?print "Hello World!",import antigravity
4. 大括號替換縮進(jìn)
Python與其他語言在語法上最大的不同就是代碼塊的結(jié)束方式,Python代碼塊簡潔許多,一個(gè)類C語言的語法:
if (x > y) {x = 1;y = 2; }在 Python 中等價(jià)的語句:
if x > y :x = 1y =2Python的目標(biāo)之一就是讓程序員少打些字,讓生活多些樂趣。Python不需要像類C語言一樣輸入 begin/end、then/endif 或者 {} 來結(jié)束代碼塊,Python使用縮進(jìn)來結(jié)束代碼塊。而braces庫算得上是Python中最惡搞的一個(gè)彩蛋了,如果你是從類C語言轉(zhuǎn)過來的程序員,估計(jì)你是一時(shí)半會兒適應(yīng)不了Python的縮進(jìn),那么是否有其他辦法呢?braces模塊就是專門為C程序員準(zhǔn)備的兼容方案。
>>> from __future__ import bracesFile "<stdin>", line 1 SyntaxError: not a chance哈哈,被騙了吧,竊以為導(dǎo)入braces就可以使用大括號來結(jié)束代碼塊,Python告訴你的答案是:沒門兒!
5. HTTPServer
如果想讓身邊的同事臨時(shí)訪問你電腦中的文件目錄,通常的做法是搭一個(gè)共享目錄出來供大家訪問,不過你要是安裝了Python,那么一切都變得簡單很多了,只需要打開命令行窗口,切換到指定目錄,執(zhí)行:
python -m SimpleHTTPServer ?# python2 python -m http.server ?# python3這是 Python 內(nèi)置的一個(gè)簡單 http server,方便自己、他人用瀏覽器來訪問你的文件目錄
6. 沒有了
關(guān)注公眾號獲取最新文章
總結(jié)
以上是生活随笔為你收集整理的好玩的Python彩蛋的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1644年,紫禁城换了三任主人
- 下一篇: python非线性可分支持向量机模型(实