Python入門之你必須了解的實(shí)用技巧
本文主要記錄 Python 中一些常用技巧,所描述的是告訴你怎么寫(xiě)才是更好? 如果你并不熟悉Python語(yǔ)法,希望你能在下面代碼片段中看到Python的簡(jiǎn)單、優(yōu)雅; 如果你象我這樣,對(duì) Python 有興趣或并正在學(xué)習(xí),我相信下面的技巧并不會(huì)讓你失望; 如果你已經(jīng)是一名 Pythoner ,那么很樂(lè)于你分享你的經(jīng)驗(yàn)和技巧。
Python 禪道
這是Python的指導(dǎo)原則,但有不同詮釋。
如果您使用的一種編程語(yǔ)言是以小品喜劇藝術(shù)團(tuán)命名的,你***有幽默感。
美麗優(yōu)于丑陋。
明確優(yōu)于含蓄。
簡(jiǎn)單比復(fù)雜好。
平倘優(yōu)于嵌套。
稀疏比密集更好。
特殊情況不能特殊到打破規(guī)則。
錯(cuò)誤不應(yīng)該默默傳遞。
......
代碼風(fēng)格: 提高可讀性
Programs must be written for people to read, and only incidentally for machines to execute.
—Abelson & Sussman, Structure and Interpretation of Computer Programs
PEP 8: Python 代碼風(fēng)格指南
值得閱讀:
http://www.python.org/dev/peps/pep-0008/
空格(行)使用 (1)
◆ 使用 4 個(gè)空格縮進(jìn)。
◆ 不要使用制表符。
◆ 不要將制表符和空格混合使用。
◆ IDEL和Emacs的Python的都支持 spaces模式。
◆ 每個(gè)函數(shù)之間應(yīng)該有一個(gè)空行。
◆ 每一個(gè) Class 之間應(yīng)該有兩個(gè)空行。
空格(行)使用 (2)
◆ 在使用 字典(dict), 列表(list), 元組(tuple), 參數(shù)(argument)列表時(shí), 應(yīng)在 "," 前添加一個(gè)空格, 并且使用字典(dict)時(shí),在 ":" 號(hào)后添加空格,而不是在前面添加。
◆ 在括號(hào)之前或參數(shù)之前不添加空格。
◆ 在文檔注釋中前后應(yīng)該沒(méi)有空格。
Python代碼
- def make_squares(key, value=0):
- """Return a dictionary and a list..."""
- d = {key: value}
- l = [key, value]
- return d, l
命名
◆ joined_lower 可以是 函數(shù)名, 方法名, 屬性名
◆ joined_lower or ALL_CAPS 是常量
◆ StudlyCaps 類名
◆ camelCase 只有在預(yù)先制定好的命名規(guī)范使用
◆ 屬性: interface, _internal, __private
◆ 但盡量避免__private形式。下面兩個(gè)鏈接解釋了 為什么python中沒(méi)有 private聲明?
http://stackoverflow.com/questions/70528/why-are-pythons-private-methods-not-actually-private
http://stackoverflow.com/questions/1641219/does-python-have-private-variables-in-classes
較長(zhǎng)代碼行
保持一行代碼在 80 個(gè)字符長(zhǎng)度。
在括號(hào)內(nèi)使用隱含的行延續(xù):
Python代碼
- def __init__(self, first, second, third,
- fourth, fifth, sixth):
- output = (first + second + third
- + fourth + fifth + sixth)
或者在需要換行的位置使用 \ 來(lái)延續(xù)行:
Python代碼
- VeryLong.left_hand_side \
- = even_longer.right_hand_side()
另外,使用反斜杠是有風(fēng)險(xiǎn)的,如果你添加一個(gè)空格在反斜杠后面,它就出錯(cuò)了。此外,它使代碼難看。
較長(zhǎng)字符串
將相鄰的字符串進(jìn)行連接的做法:
Python代碼
- >>> print 'o' 'n' "e"
- one
雖然字符之間的空格不是必需的,但是這樣有助于可讀性。
Python代碼
- >>> print 't' r'\/\/' """o"""
- t\/\/o
用一個(gè) “r“ 開(kāi)頭的字符串是一個(gè)“raw“的字符串(類似java中的轉(zhuǎn)義符)。上面的反斜杠就會(huì)當(dāng)成普通字符串處理。他們對(duì)正則表達(dá)式和Windows文件系統(tǒng)路徑非常有用。
注意:使用字符串變量名無(wú)法通過(guò)以上方式進(jìn)行連接。
Python代碼
- >>> a = 'three'
- >>> b = 'four'
- >>> a b
- File "<stdin>", line 1
- a b
- ^
- SyntaxError: invalid syntax
這是因?yàn)樽詣?dòng)連接是由Python解析器/編譯器來(lái)處理的,因?yàn)槠錈o(wú)法在編譯時(shí)對(duì)變量值進(jìn)行"翻譯",所以就這種必須在運(yùn)行時(shí)使用“+“運(yùn)算符來(lái)連接變量。
復(fù)合語(yǔ)句
Good:
Python代碼
- if foo == 'blah':
- do_something()
- do_one()
- do_two()
- do_three()
Bad:
Python代碼
- if foo == 'blah': do_something()
- do_one(); do_two(); do_three()
文檔注釋(Docstrings) & 注釋
文檔注釋 = 用于解釋如何使用代碼
文檔注釋公約:http://www.python.org/dev/peps/pep-0257/
注釋 = 為什么 (理由) & 代碼如何工作的如:
Python代碼
- # !!! BUG: ...
- # !!! FIX: This is a hack
- # ??? Why is this here?
注釋對(duì)于任何語(yǔ)言開(kāi)發(fā)者來(lái)說(shuō)已經(jīng)最基本的東西了,這里就不詳細(xì)說(shuō)了.
交換變量
在其它語(yǔ)言的交換變量的做法一般是:
Java代碼
- temp = a
- a = b
- b = temp
Python的做法:
Python代碼
- b, a = a, b
也許你見(jiàn)到過(guò)這樣的情況,但是你知道它是如何工作的嗎?
◆ 首先,逗號(hào)是元組構(gòu)造語(yǔ)法。
◆ 等號(hào)的右邊是定義一個(gè)元組 (tuple packing).
◆ 其左邊為一個(gè)目標(biāo)元組 (tuple unpacking)).
右邊的元組根據(jù)名稱被 unpacked 到左邊的無(wú)組。
更多關(guān)于 unpacked例子:
Python代碼
- >>> info =['David', 'Pythonista', '+1250']
- >>> name, title, phone = info
- >>> name
- 'Davids'
- >>> title
- 'Pythonista'
- >>> phone
- '+1250'
在結(jié)構(gòu)化的數(shù)據(jù)上使用循環(huán):
info 是在上面定義的一個(gè) list . 所以下面的 people 有兩個(gè)項(xiàng), 兩個(gè)項(xiàng)都是分別都擁有三個(gè)項(xiàng)的 list.
Python代碼
- >>> people = [info, ['Guido', 'BDFL', 'unlisted']]
- >>> for (name, title, phone) in people:
- ... print name, phone
- ...
- David +1250
- Guido unlisted
以上循環(huán)中,people中的兩個(gè)項(xiàng)(list item),都已經(jīng)被 unpacked 到 (name, title, phone) 無(wú)組中。
可以任意嵌套(只要左右兩邊的結(jié)構(gòu)一定要能夠匹配得上):
Python代碼
- >>> david, (gname, gtitle, gphone) = people
- >>> gname
- 'Guido'
- >>> gtitle
- 'BDFL'
- >>> gphone
- 'unlisted'
- >>> david
- ['David', 'Pythonista', '+1250']
#p#
更多關(guān)于 Tuples
我們看到的是元組通過(guò)逗號(hào)構(gòu)造,而不是括號(hào)。例如:
Python代碼
- >>> 1,
- (1,)
Python的解釋器會(huì)為你顯示括號(hào),所以建議你使用括號(hào):
Python代碼
- >>> (1,)
- (1,)
千萬(wàn)不要忘記逗號(hào)!
Python代碼
- >>> (1)
- 1
在只有一個(gè)元素的元組,尾隨逗號(hào)是必須的,在2 + 元素的元組,尾隨逗號(hào)是可選的。 如果創(chuàng)建一個(gè) 0或空元組,一對(duì)括號(hào)是快捷的語(yǔ)法:
Python代碼
- >>> ()
- ()
- >>> tuple()
- ()
一個(gè)常見(jiàn)的錯(cuò)誤當(dāng)你并不想要一個(gè)無(wú)組,卻無(wú)意的添加了一個(gè)逗號(hào),很容易造成你在代碼中的錯(cuò)誤,如:
Python代碼
- >>> value = 1,
- >>> value # is a tuple, not a int
- (1,)
所以,當(dāng)你發(fā)現(xiàn)一個(gè)元組時(shí),趕緊去找一下那個(gè),號(hào)吧。
關(guān)于 "_"
是一個(gè)非常有用的功能,但是卻很少有人知道。
當(dāng)你在交互式模式下(如 IDEL)計(jì)算一個(gè)表達(dá)式或調(diào)用一個(gè)函數(shù)后,其結(jié)果必然是一個(gè)臨時(shí)名稱,_(下劃線):
Python代碼
- >>> 1 + 1
- 2
- >>> _
- 2
在 _ 中存儲(chǔ)***輸出的值。
當(dāng)輸出的結(jié)果是 None 或沒(méi)有任何輸出時(shí),而 _ 的值并不會(huì)改變,仍然保存上一次的值。這就是方便所在。
當(dāng)然,這只能交互式的模式中使用,在模塊中不能支持。
這在交互式模式中是非常有用的,當(dāng)你在過(guò)程中沒(méi)有保存計(jì)算結(jié)果 或 你想看***一步的執(zhí)行的輸出結(jié)果:
Python代碼
- >>> import math
- >>> math.pi / 3
- 1.0471975511965976
- >>> angle = _
- >>> math.cos(angle)
- 0.50000000000000011
- >>> _
- 0.50000000000000011
創(chuàng)建String: 從列表中創(chuàng)建
開(kāi)始定義一個(gè) string 列表:
Python代碼
- colors = ['red', 'blue', 'green', 'yellow']
當(dāng)我們需要將上面的列表連接成一個(gè)字符串。尤其當(dāng) list 是一個(gè)很大的列表時(shí)....
不要這樣做:
Python代碼
- result = ''
- for s in colors:
- result += s
這種方式效率非常低下的,它有可怕的內(nèi)存使用問(wèn)題,至于為什么,如果你是 javaer 的話,其中的 string 連接,我想你并不陌生。
相反,你應(yīng)該這樣做:
Python代碼
- result = ''.join(colors)
當(dāng)你只有幾十或幾百個(gè)string項(xiàng)連接時(shí),它們效率上并不會(huì)太大的差別。但你要在養(yǎng)成寫(xiě)高效代碼的習(xí)慣,因?yàn)楫?dāng)字符串?dāng)?shù)千時(shí),join 比起 for 連接性能會(huì)能有所提升。
如果你需要使用一個(gè)函數(shù)來(lái)生成一個(gè)字符串列表,同樣可以使用:
Python代碼
- result = ''.join(fn(i) for i in items)
盡可能的使用
Good:
Python代碼
- for key in d:
- print key
◆ 使用 in 一般情況下是非??斓摹?/p>
◆ 這種方式也適用于其它的容器對(duì)象(如 list,tuple 和 set)。
◆ in 是操作符(正如上面所看到的)。
Bad:
Python代碼
- for key in d.keys():
- print key
保持與上面的一致性,使用 use key in dict 方式,而不是 dict.has_key():
Python代碼
- # do this:
- if key in d:
- ...do something with d[key]
- # not this:
- if d.has_key(key):
- ...do something with d[key]
#p#
字典中的 get 函數(shù)
我們經(jīng)常需要在字典中初始化數(shù)據(jù):
以下是不好的實(shí)現(xiàn)方法:
Python代碼
- navs = {}
- for (portfolio, equity, position) in data:
- if portfolio not in navs:
- navs[portfolio] = 0
- navs[portfolio] += position * prices[equity]
使用dict.get(key, default) 刪除 if 判斷代碼:
Python代碼
- navs = {}
- for (portfolio, equity, position) in data:
- navs[portfolio] = (navs.get(portfolio, 0)
- + position * prices[equity])
這種方式更為直接。
字典中的 setdefault 函數(shù) (1)
當(dāng)我們要初始化一個(gè)可變字典的值。每個(gè)字典的值將是一個(gè)列表。下面是不好的做法:
初始化可變字典的值:
Python代碼
- equities = {}
- for (portfolio, equity) in data:
- if portfolio in equities:
- equities[portfolio].append(equity)
- else:
- equities[portfolio] = [equity]
通過(guò) dict.setdefault(key, default) 使這段代碼工作的更好:
Python代碼
- equities = {}
- for (portfolio, equity) in data:
- equities.setdefault(portfolio, []).append(
- equity)
dict.setdefault()等同于“ get, or set & get“ 或"如果沒(méi)有,就設(shè)置"; 如果你的字典Key是復(fù)雜的計(jì)算或long類型,使用 setdefault 是特別有效的。
字典中的 setdefault 函數(shù) (2)
在我們看到的setdefault字典方法也可以作為一個(gè)獨(dú)立的語(yǔ)句使用:
Python代碼
- avs = {}
- for (portfolio, equity, position) in data:
- navs.setdefault(portfolio, 0)
- navs[portfolio] += position * prices[equity]
我們?cè)谶@里忽略了字典的setdefault方法返回的默認(rèn)值。我們正利用的setdefault中的作用,僅僅只是在dict中沒(méi)有 key 的值的時(shí)候才會(huì)設(shè)置。
創(chuàng)建 & 分割字典
如果你有兩份 list 對(duì)象,希望通過(guò)這兩個(gè)對(duì)象構(gòu)建一個(gè) dict 對(duì)象。
Python代碼
- given = ['John', 'Eric', 'Terry', 'Michael']
- family = ['Cleese', 'Idle', 'Gilliam', 'Palin']
- pythons = dict(zip(given, family))
- >>> pprint.pprint(pythons)
- {'John': 'Cleese',
- 'Michael': 'Palin',
- 'Eric': 'Idle',
- 'Terry': 'Gilliam'}
同樣,如果希望獲取兩份列表,也是非常簡(jiǎn)單:
Python代碼
- >>> pythons.keys()
- ['John', 'Michael', 'Eric', 'Terry']
- >>> pythons.values()
- ['Cleese', 'Palin', 'Idle', 'Gilliam']
需要注意的是,上面 list 雖然是有序的,但是 dict 中的 keys 和 values 是無(wú)序的,這正是因?yàn)?dict 本質(zhì)就是無(wú)序存儲(chǔ)的。
索引 & 項(xiàng) (1)
如果你需要一個(gè)列表,這里有一個(gè)可愛(ài)的方式來(lái)節(jié)省你的輸入:
Python代碼
- >>> items = 'zero one two three'.split()
- >>> print items
- ['zero', 'one', 'two', 'three']
如果我們需要遍歷這個(gè) list ,而且需要 index 和 item:
Python代碼
- - or -
- i = 0
- for item in items: for i in range(len(items)):
- print i, item print i, items[i]
- i += 1
索引 & 項(xiàng) (2): enumerate
通過(guò) enumerate 可以返回 list 中的 (index, item)元組:
Python代碼
- >>> print list(enumerate(items))
- [(0, 'zero'), (1, 'one'), (2, 'two'), (3, 'three')]
于是,遍歷list獲取index 及 item 就更加簡(jiǎn)單了:
Python代碼
- for (index, item) in enumerate(items):
- print index, item
Python代碼
- # compare: # compare:
- index = 0 for i in range(len(items)):
- for item in items: print i, items[i]
- print index, item
- index += 1
不難看出,使用 enumerate 比起下面兩種方式,更加簡(jiǎn)單,更加容易閱讀,這正是我們想要的。
下面是例子是如何通過(guò) enumerate 返回迭代器:
Python代碼
- >>> enumerate(items)
- <enumerate object at 0x011EA1C0>
- >>> e = enumerate(items)
- >>> e.next()
- (0, 'zero')
- >>> e.next()
- (1, 'one')
- >>> e.next()
- (2, 'two')
- >>> e.next()
- (3, 'three')
- >>> e.next()
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- StopIteration
默認(rèn)參數(shù)值
這是對(duì)于一個(gè)初學(xué)者常犯的錯(cuò)誤,甚至于一些高級(jí)開(kāi)發(fā)人員也會(huì)遇到,因?yàn)樗麄儾⒉涣私?Python 中的 names.
Python代碼
- def bad_append(new_item, a_list=[]):
- a_list.append(new_item)
- return a_list
這里的問(wèn)題是,a_list是一個(gè)空列表,默認(rèn)值是在函數(shù)定義時(shí)進(jìn)行初始化。因此,每次調(diào)用該函數(shù),你會(huì)得到不相同的默認(rèn)值。嘗試了好幾次:
Python代碼
- >>> print bad_append('one')
- ['one']
- >>> print bad_append('two')
- ['one', 'two']
列表是可變對(duì)象,你可以改變它們的內(nèi)容。正確的方式是先獲得一個(gè)默認(rèn)的列表(或dict,或sets)并在運(yùn)行時(shí)創(chuàng)建它。
Python代碼
- def good_append(new_item, a_list=None):
- if a_list is None:
- a_list = []
- a_list.append(new_item)
- return a_list
判斷 True 值
Python代碼
- # do this: # not this:
- if x: if x == True:
- pass pass
它的優(yōu)勢(shì)在于效率和優(yōu)雅。
判斷一個(gè)list:
Python代碼
- # do this: # not this:
- if items: if len(items) != 0:
- pass pass
- # and definitely not this:
- if items != []:
- pass
True 值
True和False是內(nèi)置的bool類型的布爾值的實(shí)例。誰(shuí)都只有其中的一個(gè)實(shí)例。
False | True |
False (== 0) | True (== 1) |
"" (empty string) | any string but "" (" ", "anything") |
0, 0.0 | any number but 0 (1, 0.1, -1, 3.14) |
[], (), {}, set() | any non-empty container ([0], (None,), ['']) |
None | almost any object that's not explicitly False |
簡(jiǎn)單比復(fù)雜好
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
—Brian W. Kernighan
不要重新發(fā)明輪子
在寫(xiě)任何代碼之前,
➔ ➔ ➔ ➔
檢查python 標(biāo)準(zhǔn)庫(kù).http://docs.python.org/library/index.html
檢查Python的包索引 (the "Cheese Shop"):
http://cheeseshop.python.org/pypi
Search the web. Google is your friend.
原文:[urlhttp://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html][url]
【編輯推薦】