溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中的彩蛋有哪些

發(fā)布時(shí)間:2022-01-17 15:08:10 來源:億速云 閱讀:155 作者:iii 欄目:大數(shù)據(jù)

今天小編給大家分享一下Python中的彩蛋有哪些的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

1. Hello World

相傳古時(shí)候有個(gè)退休的程序員,在家閑來無事,決定修習(xí)書法之道。第一日備好筆墨紙硯,便揮毫落紙寫下一行大字:“hello, world”。hello world 是程序員學(xué)習(xí)一門新的編程語言的開始,而Python干脆把它嵌入到了內(nèi)置模塊中。

>>> import __hello__
Hello World...

到了python3,輸出結(jié)果稍微有改動(dòng),把...替換成了!

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

參考翻譯:

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í)用性會(huì)打敗純粹性,特例也不能凌駕于規(guī)則之上。

Errors should never pass silently. Unless explicitly silenced.
除非明確地使其沉默,錯(cuò)誤永遠(yuǎn)不應(yīng)該默默地溜走。

In the face of ambiguity, refuse the temptation to guess.
面對(duì)不明確的定義,拒絕猜測的誘惑。

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è)朋友好奇的問他怎么飛起來的。
Python中的彩蛋有哪些
這個(gè)圖的對(duì)話是:

Friend:哇!你在飛,怎么做到的?
Cueball:Python!
Cueball:我昨晚學(xué)的,非常簡單
Cueball:運(yùn)行 Hello World 只需要一句話:print "Hello World!",
Friend: 我不懂什么動(dòng)態(tài)類型、空白符之類的?
Cueball:來,加入我們吧!Python編程很有樂趣,它是一個(gè)全新的世界
Friend: 但你是怎么飛起來的?
Cueball: 我就只是輸入了 import antigravity
Friend: 就這樣? (還是一臉不惑的表情)
Cueball: …嗯,我也對(duì)比了藥品柜中的所有東西 (暗指Cueball對(duì)比過多種編程語言,但還是覺得Python最簡單)
Cueball: 但我想這就是Python

4. 大括號(hào)替換縮進(jìn)

Python與其他語言在語法上最大的不同就是代碼塊的結(jié)束方式,Python代碼塊簡潔許多,一個(gè)類C語言的語法:

if (x > y) {
    x = 1;
    y = 2;
}

在 Python 中等價(jià)的語句:

if x > y :
    x = 1
    y =2

Python的目標(biāo)之一就是讓程序員少打些字,讓生活多些樂趣。Python不需要像類C語言一樣輸入 begin/endthen/endif 或者 {} 來結(jié)束代碼塊,Python使用縮進(jìn)來結(jié)束代碼塊。而braces庫算得上是Python中最惡搞的一個(gè)彩蛋了,如果你是從類C語言轉(zhuǎn)過來的程序員,估計(jì)你是一時(shí)半會(huì)兒適應(yīng)不了Python的縮進(jìn),那么是否有其他辦法呢?braces模塊就是專門為C程序員準(zhǔn)備的兼容方案。

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

哈哈,被騙了吧,竊以為導(dǎo)入braces就可以使用大括號(hào)來結(jié)束代碼塊,Python告訴你的答案是:沒門兒!

5. HTTPServer

如果想讓身邊的同事臨時(shí)訪問你電腦中的文件目錄,通常的做法是搭一個(gè)共享目錄出來供大家訪問,不過你要是安裝了Python,那么一切都變得簡單很多了,只需要打開命令行窗口,切換到指定目錄,執(zhí)行:

python -m SimpleHTTPServer  # python2
python -m http.server  # python3

這是 Python 內(nèi)置的一個(gè)簡單 http server,方便自己、他人用瀏覽器來訪問你的文件目錄

以上就是“Python中的彩蛋有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI