鍍金池/ 教程/ Python/ 對(duì)象變動(dòng)(Mutation)
<code>open</code>函數(shù)
Python 2系列版本
可迭代對(duì)象(Iterable)
異常
在函數(shù)中嵌入裝飾器
你的第一個(gè)裝飾器
上下文管理器(Context managers)
<code>set</code>(集合)數(shù)據(jù)結(jié)構(gòu)
裝飾器類
字典推導(dǎo)式(<code>dict</code> comprehensions)
<code>Reduce</code>
捐贈(zèng)名單
<code>Filter</code>
<code>try/else</code>從句
*args 的用法
<code>dir</code>
處理異常
<code>else</code>從句
對(duì)象自省
For - Else
18. 一行式
Python 3.2及以后版本
Global和Return
基于類的實(shí)現(xiàn)
容器(<code>Collections</code>)
23. 協(xié)程
推薦閱讀
譯者后記
<code>*args</code> 和 <code>**kwargs</code>
**kwargs 的用法
生成器(Generators)
迭代(Iteration)
基于生成器的實(shí)現(xiàn)
將函數(shù)作為參數(shù)傳給另一個(gè)函數(shù)
日志(Logging)
三元運(yùn)算符
<code>inspect</code>模塊
枚舉
Map,F(xiàn)ilter 和 Reduce
各種推導(dǎo)式(comprehensions)
從函數(shù)中返回函數(shù)
列表推導(dǎo)式(<code>list</code> comprehensions)
處理多個(gè)異常
帶參數(shù)的裝飾器
對(duì)象變動(dòng)(Mutation)
22. 目標(biāo)Python2+3
迭代器(Iterator)
虛擬環(huán)境(virtualenv)
<code>__slots__</code>魔法
什么時(shí)候使用它們?
Python/C API
<code>Map</code>
SWIG
授權(quán)(Authorization)
裝飾器
一切皆對(duì)象
使用C擴(kuò)展
使用 <code>*args</code> 和 <code>**kwargs</code> 來(lái)調(diào)用函數(shù)
17. <code>lambda</code>表達(dá)式
集合推導(dǎo)式(<code>set</code> comprehensions)
<code>type</code>和<code>id</code>
在函數(shù)中定義函數(shù)
<code>finally</code>從句
CTypes
調(diào)試(Debugging)
使用場(chǎng)景
生成器(Generators)
多個(gè)return值
關(guān)于原作者
函數(shù)緩存 (Function caching)
Python進(jìn)階

對(duì)象變動(dòng)(Mutation)

Python中可變(mutable)與不可變(immutable)的數(shù)據(jù)類型讓新手很是頭痛。簡(jiǎn)單的說(shuō),可變(mutable)意味著"可以被改動(dòng)",而不可變(immutable)的意思是“常量(constant)”。想把腦筋轉(zhuǎn)動(dòng)起來(lái)嗎?考慮下這個(gè)例子:

foo = ['hi']
print(foo)
# Output: ['hi']

bar = foo
bar += ['bye']
print(foo)
# Output: ['hi', 'bye']

剛剛發(fā)生了什么?我們預(yù)期的不是那樣!我們期望看到是這樣的:

foo = ['hi']
print(foo)
# Output: ['hi']

bar = foo
bar += ['bye']

print(foo)
# Output: ['hi']

print(bar)
# Output: ['hi', 'bye']

這不是一個(gè)bug。這是對(duì)象可變性(mutability)在作怪。每當(dāng)你將一個(gè)變量賦值為另一個(gè)可變類型的變量時(shí),對(duì)這個(gè)數(shù)據(jù)的任意改動(dòng)會(huì)同時(shí)反映到這兩個(gè)變量上去。新變量只不過(guò)是老變量的一個(gè)別名而已。這個(gè)情況只是針對(duì)可變數(shù)據(jù)類型。下面的函數(shù)和可變數(shù)據(jù)類型讓你一下就明白了:

def add_to(num, target=[]):
    target.append(num)
    return target

add_to(1)
# Output: [1]

add_to(2)
# Output: [1, 2]

add_to(3)
# Output: [1, 2, 3]

你可能預(yù)期它表現(xiàn)的不是這樣子。你可能希望,當(dāng)你調(diào)用add_to時(shí),有一個(gè)新的列表被創(chuàng)建,就像這樣:

def add_to(num, target=[]):
    target.append(num)
    return target

add_to(1)
# Output: [1]

add_to(2)
# Output: [2]

add_to(3)
# Output: [3]

啊哈!這次又沒有達(dá)到預(yù)期,是列表的可變性在作怪。在Python中當(dāng)函數(shù)被定義時(shí),默認(rèn)參數(shù)只會(huì)運(yùn)算一次,而不是每次被調(diào)用時(shí)都會(huì)重新運(yùn)算。你應(yīng)該永遠(yuǎn)不要定義可變類型的默認(rèn)參數(shù),除非你知道你正在做什么。你應(yīng)該像這樣做:

def add_to(element, target=None):
    if target is None:
        target = []
    target.append(element)
    return target

現(xiàn)在每當(dāng)你在調(diào)用這個(gè)函數(shù)不傳入target參數(shù)的時(shí)候,一個(gè)新的列表會(huì)被創(chuàng)建。舉個(gè)例子:

add_to(42)
# Output: [42]

add_to(42)
# Output: [42]

add_to(42)
# Output: [42]