Python源碼理解: '+=' 和 'xx = xx + xx'的區別
前菜
在我們使用Python的過程, 很多時候會用到 + 運算, 例如:
a = 1 + 2
print a
輸出
3</code></pre>
不光在加法中使用, 在字符串的拼接也同樣發揮這重要的作用, 例如:
a = 'abc' + 'efg'
print a
輸出
abcefg</code></pre>
同樣的, 在列表中也能使用, 例如:
a = [1, 2, 3] + [4, 5, 6]
print a
輸出
[1, 2, 3, 4, 5, 6]</code></pre>
為什么上面不同的對象執行同一個 + 會有不同的效果呢? 這就涉及到 + 的重載, 然而這不是本文要討論的重點, 上面的只是前菜而已~~~
正文
先看一個例子:
num = 123
num = num + 4
print num
輸出
127</code></pre>
這段代碼的用途很明確, 就是一個簡單的數字相加, 但是這樣似乎很繁瑣, 一點都Pythonic, 于是就有了下面的代碼:
num = 123
num += 4
print num
輸出
127</code></pre>
哈, 這樣就很Pythonic了! 但是這種用法真的就是這么好么? 不一定. 看例子:
# coding: utf8
l = [1, 2]
l = l + [3, 4]
print l
輸出
[1, 2, 3, 4]
------------------------------------------
l = [1, 2]
l += [3, 4] # 列表的+被重載了, 左右操作數必須都是iterable對象, 否則會報錯
print l
輸出
[1, 2, 3, 4]</code></pre>
看起來結果都一樣嘛~, 但是真的一樣嗎? 我們改下代碼再看下:
# coding: utf8
l = [1, 2]
print 'l之前的id: ', id(l)
l = l + [3, 4]
print 'l之后的id: ', id(l)
輸出
l之前的id: 40270024
l之后的id: 40389000
------------------------------------------
l = [1, 2]
print 'l之前的id: ', id(l)
l += [3, 4] # 列表的+被重載了, 左右操作數必須都是iterable對象, 否則會報錯
print 'l之后的id: ', id(l)
輸出
l之前的id: 40270024
l之后的id: 40270024</code></pre>
看到結果了嗎? 雖然結果一樣, 但是通過 id 的值表示, 運算前后, 第一種方法對象是不同的了, 而第二種還是同一個對象! 為什么會這樣?
結果分析
先來看看字節碼:
[root@test1 ~]# cat 2.py
coding: utf8
l = [1, 2]
l = l + [3, 4]
print l
l = [1, 2]
l += [3, 4]
print l
[root@test1 ~]# python -m dis 2.py
2 0 LOAD_CONST 0 (1)
3 LOAD_CONST 1 (2)
6 BUILD_LIST 2
9 STORE_NAME 0 (l)
3 12 LOAD_NAME 0 (l)
15 LOAD_CONST 2 (3)
18 LOAD_CONST 3 (4)
21 BUILD_LIST 2
24 BINARY_ADD
25 STORE_NAME 0 (l)
4 28 LOAD_NAME 0 (l)
31 PRINT_ITEM
32 PRINT_NEWLINE
7 33 LOAD_CONST 0 (1)
36 LOAD_CONST 1 (2)
39 BUILD_LIST 2
42 STORE_NAME 0 (l)
8 45 LOAD_NAME 0 (l)
48 LOAD_CONST 2 (3)
51 LOAD_CONST 3 (4)
54 BUILD_LIST 2
57 INPLACE_ADD
58 STORE_NAME 0 (l)
9 61 LOAD_NAME 0 (l)
64 PRINT_ITEM
65 PRINT_NEWLINE
66 LOAD_CONST 4 (None)
69 RETURN_VALUE</code></pre>
在上訴的字節碼, 我們著重需要看的是兩個: BINARY_ADD 和 INPLACE_ADD ! 很明顯:
l = l + [3, 4, 5] 這種背后就是 BINARY_ADD
l += [3, 4, 5] 這種背后就是 INPLACE_ADD
深入理解
雖然兩個單詞差很遠, 但其實兩個的作用是很類似的, 最起碼前面一部分是, 為什么這樣說, 請看源碼:
# 取自ceva.c
BINARY_ADD
TARGET_NOARG(BINARY_ADD)
{
w = POP();
v = TOP();
if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { // 檢查左右操作數是否 int 類型
/ INLINE: int + int /
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
/ cast to avoid undefined behaviour
on overflow /
i = (long)((unsigned long)a + b);
if ((i^a) < 0 && (i^b) < 0)
goto slow_add;
x = PyInt_FromLong(i);
}
else if (PyString_CheckExact(v) &&
PyString_CheckExact(w)) { // 檢查左右操作數是否 string 類型
x = string_concatenate(v, w, f, next_instr);
/ string_concatenate consumed the ref to v /
goto skip_decref_vx;
}
else {
slow_add: // 兩者都不是, 請走這里~
x = PyNumber_Add(v, w);
}
...(省略)
INPLACE_ADD
TARGET_NOARG(INPLACE_ADD)
{
w = POP();
v = TOP();
if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) { // 檢查左右操作數是否 int 類型
/ INLINE: int + int /
register long a, b, i;
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
i = a + b;
if ((i^a) < 0 && (i^b) < 0)
goto slow_iadd;
x = PyInt_FromLong(i);
}
else if (PyString_CheckExact(v) &&
PyString_CheckExact(w)) { // 檢查左右操作數是否 string 類型
x = string_concatenate(v, w, f, next_instr);
/ string_concatenate consumed the ref to v /
goto skip_decref_v;
}
else {
slow_iadd:
x = PyNumber_InPlaceAdd(v, w); // 兩者都不是, 請走這里~
}
... (省略)</code></pre>
從上面可以看出, 不管是 BINARY_ADD 還是 INPLACE_ADD , 他們都會有如下相同的操作:
檢查是不是都是`int`類型, 如果是, 直接返回兩個數值相加的結果
檢查是不是都是`string`類型, 如果是, 直接返回字符串拼接的結果
因為兩者的行為真的很類似, 所以在這著重講 INPLACE_ADD , 對 BINARY_ADD 感興趣的童鞋可以在源碼文件: abstract.c , 搜索: PyNumber_Add .實際上也就少了對列表之類對象的操作而已.
那我們接著繼續, 先貼個源碼:
PyObject *
PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
{
PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
NB_SLOT(nb_add));
if (result == Py_NotImplemented) {
PySequenceMethods *m = v->ob_type->tp_as_sequence;
Py_DECREF(result);
if (m != NULL) {
binaryfunc f = NULL;
if (HASINPLACE(v))
f = m->sq_inplace_concat;
if (f == NULL)
f = m->sq_concat;
if (f != NULL)
return (*f)(v, w);
}
result = binop_type_error(v, w, "+=");
}
return result;
INPLACE_ADD 本質上是對應著 abstract.c 文件里面的 PyNumber_InPlaceAdd 函數, 在這個函數中, 首先調用 binary_iop1 函數, 然后進而又調用了里面的 binary_op1 函數, 這兩個函數很大一個篇幅, 都是針對 ob_type->tp_as_number , 而我們目前是 list , 所以他們的大部分操作, 都和我們的無關. 正因為無關, 所以這兩函數調用最后, 直接返回 Py_NotImplemented , 而這個是用來干嘛, 這個有大作用, 是列表相加的核心所在!
因為 binary_iop1 的調用結果是 Py_NotImplemented , 所以下面的判斷成立, 開始尋找對象( 也就是演示代碼中l對象 )的 ob_type->tp_as_sequence 屬性.
因為我們的對象是l(列表), 所以我們需要去 PyList_type 需找真相:
# 取自: listobject.c
PyTypeObject PyList_Type = {
... (省略)
&list_as_sequence, /* tp_as_sequence */
... (省略)
}
可以看出, 其實也就是直接取 list_as_sequence , 而這個是什么呢? 其實是一個結構體, 里面存放了列表的部分功能函數.
static PySequenceMethods list_as_sequence = {
(lenfunc)list_length, /* sq_length */
(binaryfunc)list_concat, /* sq_concat */
(ssizeargfunc)list_repeat, /* sq_repeat */
(ssizeargfunc)list_item, /* sq_item */
(ssizessizeargfunc)list_slice, /* sq_slice */
(ssizeobjargproc)list_ass_item, /* sq_ass_item */
(ssizessizeobjargproc)list_ass_slice, /* sq_ass_slice */
(objobjproc)list_contains, /* sq_contains */
(binaryfunc)list_inplace_concat, /* sq_inplace_concat */
(ssizeargfunc)list_inplace_repeat, /* sq_inplace_repeat */
};
接下來就是一個判斷, 判斷咱們這個 l 對象是否有 Py_TPFLAGS_HAVE_INPLACEOPS 這個特性, 很明顯是有的, 所以就調用上步取到的結構體中的 sq_inplace_concat 函數, 那接下來呢? 肯定就是看看這個函數是干嘛的:
list_inplace_concat(PyListObject self, PyObject other)
{
PyObject *result;
result = listextend(self, other); # 關鍵所在
if (result == NULL)
return result;
Py_DECREF(result);
Py_INCREF(self);
return (PyObject *)self;
}</code></pre>
終于找到關鍵了, 原來最后就是調用這個 listextend 函數, 這個和我們 python 層面的列表的 extend方法 很類似, 在這不細講了!
把 PyNumber_InPlaceAdd 的執行調用過程, 簡單整理下來就是:
INPLACE_ADD(字節碼)
-> PyNumber_InPlaceAdd
-> 判斷是否數字: 如果是, 直接返回兩數相加
-> 判斷是否字符串: 如果是, 直接返回`string_concatenate`的結果
-> 都不是:
-> binary_iop1 (判斷是否數字, 如果是則按照數字處理, 否則返回Py_NotImplemented)
-> binary_iop (判斷是否數字, 如果是則按照數字處理, 否則返回Py_NotImplemented)
-> 返回的結果是否 Py_NotImplemented:
-> 是:
-> 對象是否有Py_TPFLAGS_HAVE_INPLACEOPS:
-> 是: 調用對象的: sq_inplace_concat
-> 否: 調用對象的: sq_concat
-> 否: 報錯
所以在上面的結果, 第二種代碼: l += [3,4,5] , 我們看到的 id 值并沒有改變, 就是因為 += 通過 sq_inplace_concat 調用了列表的 listextend 函數, 然后導致新列表以追加的方式去處理.
結論
現在我們大概明白了 += 實際上是干嘛了: 它應該能算是一個加強版的 + , 因為它比 + 多了一個寫回本身的功能.不過是否能夠寫回本身, 還是得看對象自身是否支持, 也就是說是否具備 Py_NotImplemented 標識, 是否支持 sq_inplace_concat , 如果具備, 才能實現, 否則, 也就是和 + 效果一樣而已.
來自:https://segmentfault.com/a/1190000009764209