python元组

In [8]: t = (1,2, [30, 40])

In [9]: t[2]
Out[9]: [30, 40]

In [10]: t[2] += [50,60]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-fb586dd7f384> in <module>()
----> 1 t[2] += [50,60]

TypeError: 'tuple' object does not support item assignment

In [11]: t
Out[11]: (1, 2, [30, 40, 50, 60])

In [12]: t[2]
Out[12]: [30, 40, 50, 60]

In [13]: t[2].append(70)

In [14]: t
Out[14]: (1, 2, [30, 40, 50, 60, 70])

看下list的append和+=

# test.py
a = [1, 2, 3]
a.append(4)
a += [5]
a = a + [6]

 

E:\workspace\tutorial>python -m dis test.py
  1           0 LOAD_CONST               0 (1)
              3 LOAD_CONST               1 (2)
              6 LOAD_CONST               2 (3)
              9 BUILD_LIST               3
             12 STORE_NAME               0 (a)

  2          15 LOAD_NAME                0 (a)
             18 LOAD_ATTR                1 (append)
             21 LOAD_CONST               3 (4)
             24 CALL_FUNCTION            1
             27 POP_TOP

  3          28 LOAD_NAME                0 (a)
             31 LOAD_CONST               4 (5)
             34 BUILD_LIST               1
             37 INPLACE_ADD
             38 STORE_NAME               0 (a)

  4          41 LOAD_NAME                0 (a)
             44 LOAD_CONST               5 (6)
             47 BUILD_LIST               1
             50 BINARY_ADD
             51 STORE_NAME               0 (a)
             54 LOAD_CONST               6 (None)
             57 RETURN_VALUE

 

posted on 2017-08-19 21:16  漫步的影子  阅读(178)  评论(0编辑  收藏  举报