瞧瞧,這樣的代碼才叫 Pythonic
Python由于語(yǔ)言的簡(jiǎn)潔性,讓我們以人類思考的方式來(lái)寫代碼,新手更容易上手,老鳥更愛不釋手。
要寫出 Pythonic(優(yōu)雅的、地道的、整潔的)代碼,還要平時(shí)多觀察那些大牛代碼,這里明哥收集了一些比較常見的 Pythonic 寫法,幫助你養(yǎng)成寫優(yōu)秀代碼的習(xí)慣。
1. 變量交換
交換兩個(gè)變量的值,正常都會(huì)想利用一個(gè)中間臨時(shí)變量來(lái)過(guò)渡。
- tmp = a
- a = b
- b = tmp
能用一行代碼解決的(并且不影響可讀性的),決不用三行代碼。
- a,bb = b,a
2. 列表推導(dǎo)
下面是一個(gè)非常簡(jiǎn)單的 for 循環(huán)。
- my_list = []
- for i in range(10):
- my_list.append(i*2)
在一個(gè) for 循環(huán)中,如果邏輯比較簡(jiǎn)單,不如試用一下列表的列表推導(dǎo)式,雖然只有一行代碼,但也邏輯清晰。
- my_list = [i*2 for i in range(10)]
3. 單行表達(dá)式
上面兩個(gè)案例,都將多行代碼用另一種方式寫成了一行代碼。
這并不意味著,代碼行數(shù)越少,就越 Pythonic 。
比如下面這樣寫,就不推薦。
- print('hello'); print('world')
- if x == 1: print('hello,world')
- if <complex comparison> and <other complex comparison>:
- # do something
建議還是按照如下的寫法來(lái)
- print('hello')
- print('world')
- if x == 1:
- print('hello,world')
- cond1 = <complex comparison>
- cond2 = <other complex comparison>
- if cond1 and cond2:
- # do something
4. 帶索引遍歷
使用 for 循環(huán)時(shí),如何取得對(duì)應(yīng)的索引,初學(xué)者習(xí)慣使用 range + len 函數(shù)
- for i in range(len(my_list)):
- print(i, "-->", my_list[i])
更好的做法是利用 enumerate 這個(gè)內(nèi)置函數(shù)
- for i,item in enumerate(my_list):
- print(i, "-->",item)
5. 序列解包
使用 * 可以對(duì)一個(gè)列表解包
- a, *rest = [1, 2, 3]
- # a = 1, rest = [2, 3]
- a, *middle, c = [1, 2, 3, 4]
- # a = 1, middle = [2, 3], c = 4
6. 字符串拼接
如果一個(gè)列表(或者可迭代對(duì)象)中的所有元素都是字符串對(duì)象,想要將他們連接起來(lái),通常做法是
- letters = ['s', 'p', 'a', 'm']
- s=""
- for let in letters:
- s += let
更推薦的做法是使用 join 函數(shù)
- letters = ['s', 'p', 'a', 'm']
- word = ''.join(letters)
7. 真假判斷
判斷一個(gè)變量是否為真(假),新手習(xí)慣直接使用 == 與 True、False、None 進(jìn)行對(duì)比
- if attr == True:
- print('True!')
- if attr == None:
- print('attr is None!')
實(shí)際上,""、[]、{} 這些沒有任何元素的容器都是假值,可直接使用 if not xx 來(lái)判斷。
- if attr:
- print('attr is truthy!')
- if not attr:
- print('attr is falsey!')
8. 訪問(wèn)字典元素
當(dāng)直接使用 [] 來(lái)訪問(wèn)字典里的元素時(shí),若key不存在,是會(huì)拋異常的,所以新會(huì)可能會(huì)先判斷一下是否有這個(gè) key,有再取之。
- d = {'hello': 'world'}
- if d.has_key('hello'):
- print(d['hello']) # prints 'world'
- else:
- print('default_value')
更推薦的做法是使用 get 來(lái)取,如果沒有該 key 會(huì)默認(rèn)返回 None(當(dāng)然你也可以設(shè)置默認(rèn)返回值)
- d = {'hello': 'world'}
- print(d.get('hello', 'default_value')) # prints 'world'
- print(d.get('thingy', 'default_value')) # prints 'default_value'
9. 操作列表
下面這段代碼,會(huì)根據(jù)條件過(guò)濾過(guò)列表中的元素
- a = [3, 4, 5]
- b = []
- for i in a:
- if i > 4:
- b.append(i)
實(shí)際上可以使用列表推導(dǎo)或者高階函數(shù) filter 來(lái)實(shí)現(xiàn)
- a = [3, 4, 5]
- b = [i for i in a if i > 4]
- # Or:
- b = filter(lambda x: x > 4, a)
除了 filter 之外,還有 map、reduce 這兩個(gè)函數(shù)也很好用
- a = [3, 4, 5]
- b = map(lambda i: i + 3, a)
- # b: [6,7,8]
10. 文件讀取
文件讀取是非常常用的操作,在使用完句柄后,是需要手動(dòng)調(diào)用 close 函數(shù)來(lái)關(guān)閉句柄的
- fp = open('file.txt')
- print(fp.read())
- fp.close()
如果代碼寫得太長(zhǎng),即使你知道需要手動(dòng)關(guān)閉句柄,卻也會(huì)經(jīng)常會(huì)漏掉。因此推薦養(yǎng)成習(xí)慣使用 with open 來(lái)讀寫文件,上下文管理器會(huì)自動(dòng)執(zhí)行關(guān)閉句柄的操作
- with open('file.txt') as fp:
- for line in fp.readlines():
- print(line)
11. 代碼續(xù)行
將一個(gè)長(zhǎng)度較長(zhǎng)的字符串放在一行中,是很影響代碼可讀性的(下面代碼可向左滑動(dòng))
- long_string = 'For a long time I used to go to bed early. Sometimes, when I had put out my candle, my eyes would close so quickly that I had not even time to say “I’m going to sleep.”'
稍等注重代碼可讀性的人,會(huì)使用三個(gè)引號(hào) \來(lái)續(xù)寫
- long_string = 'For a long time I used to go to bed early. ' \
- 'Sometimes, when I had put out my candle, ' \
- 'my eyes would close so quickly that I had not even time to say “I’m going to sleep.”'
不過(guò),對(duì)我來(lái)說(shuō),我更喜歡這樣子寫 使用括號(hào)包裹 ()
- long_string = (
- "For a long time I used to go to bed early. Sometimes, "
- "when I had put out my candle, my eyes would close so quickly "
- "that I had not even time to say “I’m going to sleep.”"
- )
導(dǎo)包的時(shí)候亦是如此
- from some.deep.module.inside.a.module import (
- a_nice_function, another_nice_function, yet_another_nice_function)
12. 顯式代碼
有時(shí)候出于需要,我們會(huì)使用一些特殊的魔法來(lái)使代碼適應(yīng)更多的場(chǎng)景不確定性。
- def make_complex(*args):
- x, y = args
- return dict(**locals())
但若非必要,請(qǐng)不要那么做。無(wú)端增加代碼的不確定性,會(huì)讓原先本就動(dòng)態(tài)的語(yǔ)言寫出更加動(dòng)態(tài)的代碼。
- def make_complex(x, y):
- return {'x': x, 'y': y}
13. 使用占位符
對(duì)于暫不需要,卻又不得不接收的的變量,請(qǐng)使用占位符
- filename = 'foobar.txt'
- basename, _, ext = filename.rpartition('.')
14. 鏈?zhǔn)奖容^
對(duì)于下面這種寫法
- score = 85
- if score > 80 and score < 90:
- print("良好")
其實(shí)還有更好的寫法
- score = 85
- if 80 < score < 90:
- print("良好")
如果你理解了上面的鏈?zhǔn)奖容^操作,那么你應(yīng)該知道為什么下面這行代碼輸出的結(jié)果是 False
- >>> False == False == True
- False
15. 三目運(yùn)算
對(duì)于簡(jiǎn)單的判斷并賦值
- age = 20
- if age > 18:
- type = "adult"
- else:
- type = "teenager"
其實(shí)是可以使用三目運(yùn)算,一行搞定。
- age = 20
- b = "adult" if age > 18 else "teenager"