Python 初學(xué)者容易踩的五個(gè)坑
哈嘍大家好,我是咸魚。
今天咸魚列出了一些大家在初學(xué) Python 的時(shí)候容易踩的一些坑,看看你有沒有中招過。
參考文章:https://www.bitecode.dev/p/unexpected-python-traps-for-beginners
不明顯的字符串拼接
Python 在詞法分析的時(shí)候會(huì)把多個(gè)字符串自動(dòng)拼接起來。
data = "very""lazy"
print(data) # verylazy
這個(gè)特性可以讓我們?cè)诼暶饕粋€(gè)長(zhǎng)字符串的時(shí)候可以分成多行來寫,這樣看起來比較優(yōu)雅。
msg = (
"I want this to be on a single line when it prints "
"but I want it to be broken into several lines in "
"the code"
)
print(msg)
# I want this to be on a single line when it prints but I want it to be broken into several lines in the code
msg ="I want this to be on a single line when it prints " \
"but I want it to be broken into several lines in " \
"the code"
print(msg)
# I want this to be on a single line when it prints but I want it to be broken into several lines in the code
但初學(xué)者往往會(huì)忽略這一點(diǎn),他們?cè)谑褂冒址牧斜頃r(shí)把分隔符漏掉,造成了意想不到的字符串拼接。
比如說想要聲明一個(gè)包含域名的列表host。
host = [
"localhost",
"bitecode.dev",
"www.bitecode.dev"
]
print(host) # ['localhost', 'bitecode.dev', 'www.bitecode.dev']
但是寫成了下面這樣。
host = [
"localhost",
"127.0.0.1",
"bitecode.dev" # 這里把逗號(hào)忘掉了
"www.bitecode.dev"
]
print(host) # ['localhost', 'bitecode.devwww.bitecode.dev']
這是有效的代碼,不會(huì)觸發(fā)語法錯(cuò)誤,但是解析的時(shí)候會(huì)把 "bitecode.dev" 和 "www.bitecode.dev" 拼接在一起,變成 'bitecode.devwww.bitecode.dev' 。
sorted() 和 .sort() 傻傻分不清
在 Python 中,大多數(shù)函數(shù)或方法都會(huì)返回一個(gè)值。比如說我們要對(duì)一個(gè)列表里面的內(nèi)容進(jìn)行排序,可以使用 sorted() 方法。
# sorted() 方法會(huì)返回一個(gè)排序后的新列表
numbers = [4, 2, 3]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # [2, 3, 4]
我們也可以用列表自帶的 .sort() 方法來排序,需要注意的是:.sort() 直接對(duì)原有列表進(jìn)行排序,不會(huì)返回任何值。
# .sort() 方法直接對(duì)原列表進(jìn)行排序
numbers = [4, 2, 3]
numbers.sort()
print(numbers) # [2, 3, 4]
但是初學(xué)者很容易把 sorted() 的用法用在 .sort() 上,結(jié)果發(fā)現(xiàn)怎么返回了一個(gè) None。
numbers = [4, 2, 3]
sorted_numbers = numbers.sort()
print(sorted_numbers) # None
list.sort() 修改原列表,它不會(huì)返回任何內(nèi)容。當(dāng) Python 可調(diào)用對(duì)象不返回任何內(nèi)容時(shí),會(huì)得到 None 。
或者把 .sort() 的用法用在了 sorted() 上。
numbers = [4, 2, 3]
sorted(numbers)
print(numbers) # [4, 2, 3]
不要亂加尾隨逗號(hào)
我們?cè)趧?chuàng)建一個(gè)空元組的時(shí)候可以用下面的兩種方法:
t1 = ()
t2 = tuple()
print(type(t1))
print(type(t2))
在 Python 中,雖然元組通常都是使用一對(duì)小括號(hào)將元素包圍起來的,但是小括號(hào)不是必須的,只要將各元素用逗號(hào)隔開,Python 就會(huì)將其視為元組。
t1 = 1,
print(t1) # (1,)
print(type(t1)) # <class 'tuple'>
所以如果在數(shù)據(jù)后面多加了一個(gè)逗號(hào),就會(huì)產(chǎn)生一些問題。
比如說下面是一個(gè)列表:
colors = [
'red',
'blue',
'green',
]
print(colors) # ['red', 'blue', 'green']
如果不小心加了一個(gè)尾隨逗號(hào),列表就變成了元組。
colors = [
'red',
'blue',
'green',
],
print(colors) # (['red', 'blue', 'green'],)
在 python 中,包含一個(gè)元素的元組必須有逗號(hào),比如下面是包含一個(gè)列表的元組:
colors = [
'red',
'blue',
'green',
],
這是列表:
colors = ([
'red',
'blue',
'green',
])
可怕的 is
在 python 中, is 和 == 都是用來比較 python 對(duì)象的,區(qū)別是:
- is 比較需要對(duì)象的值和內(nèi)存地址都相等
- == 比較只需要對(duì)象的值相等就行了
事實(shí)上,這兩者的實(shí)際使用要遠(yuǎn)遠(yuǎn)復(fù)雜得多。
比如說下面的 a 和 b 是兩個(gè)不同的對(duì)象,a is b 應(yīng)該返回 False,但是卻返回了 True
a = 4
b = 4
print(a == b) # True
print(a is b) # True
在 python 中,由于小整數(shù)池和緩存機(jī)制,使用 is 來比較對(duì)象往往會(huì)出現(xiàn)意想不到的結(jié)果。
關(guān)于小整數(shù)池和緩存機(jī)制可以看我這篇文章:《Python 中 is 和 == 的區(qū)別》
奇怪的引用
在Python中,如果 * 運(yùn)算符用于數(shù)字與非數(shù)字型數(shù)據(jù)(列表、字符串、元組等)的結(jié)合,它將重復(fù)非數(shù)字型數(shù)據(jù)。
print("0" * 3) # '000'
print((0,) * 3) # (0, 0, 0)
在創(chuàng)建一個(gè)多個(gè)列表元素的元組時(shí)候,如果使用下面的代碼:
t1 = ([0],) * 3
print(t1) # ([0], [0], [0])
會(huì)帶來意想不到的問題:我們對(duì)元組中的第一個(gè)列表元素中的數(shù)據(jù)進(jìn)行算數(shù)運(yùn)算(自增 1)
t1[0][0] += 1
print(t1) # ([1], [1], [1])
我們發(fā)現(xiàn)元組中的所有列表元素內(nèi)的數(shù)據(jù)都自增 1 了,我們不是只對(duì)第一個(gè)列表元素進(jìn)行自增的嗎?
實(shí)際上,當(dāng)我們執(zhí)行 t1 = ([0],) * 3 的時(shí)候,不會(huì)創(chuàng)建一個(gè)包含三個(gè)列表組成的元組,而是創(chuàng)建一個(gè)包含 3 個(gè) 引用的元組,每個(gè)引用都指向同一個(gè)列表。
元組中的每個(gè)元素都是對(duì)同一個(gè)可變對(duì)象(列表)的引用,所以當(dāng)我們修改其中的元素時(shí),另外的對(duì)象也會(huì)跟著發(fā)生變化。
正確的方法應(yīng)該是:
t2 = ([0], [0], [0])
# 或者 t2 = tuple([0] for _ in range(3))
t2[0][0] += 1
print(t2) # ([1], [0], [0])
在 python 的其他地方中也有這種類似的坑
def a_bugged_function(reused_list=[]):
reused_list.append("woops")
return reused_list
print(a_bugged_function()) # ['woops']
print(a_bugged_function()) # ['woops', 'woops']
print(a_bugged_function()) # ['woops', 'woops', 'woops']
可以看到,reused_list 在函數(shù)定義中被初始化為一個(gè)空列表 [],然后每次函數(shù)調(diào)用時(shí)都使用這個(gè)默認(rèn)的空列表。
在第一次調(diào)用 a_bugged_function() 后,列表變成了 ['woops']。然后,在第二次和第三次調(diào)用中,它分別繼續(xù)被修改,導(dǎo)致輸出的結(jié)果為:
['woops']
['woops', 'woops']
['woops', 'woops', 'woops']
這是因?yàn)樵诤瘮?shù)定義中,如果將可變對(duì)象(例如列表)作為默認(rèn)參數(shù),會(huì)導(dǎo)致該對(duì)象在函數(shù)調(diào)用時(shí)被共享和修改:每次調(diào)用函數(shù)時(shí),使用的都是同一個(gè)列表對(duì)象的引用。
為了避免這種情況,常見的做法是使用不可變對(duì)象(如 None)作為默認(rèn)值,并在函數(shù)內(nèi)部根據(jù)需要?jiǎng)?chuàng)建新的可變對(duì)象。
def a_fixed_function(reused_list=None):
if reused_list is None:
reused_list = []
reused_list.append("woops")
return reused_list
print(a_fixed_function())
print(a_fixed_function())
print(a_fixed_function())