日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中a=a+2与a+=2的区别

發布時間:2025/3/21 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中a=a+2与a+=2的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、a=a+2,表示一個新的對象,新的對象名字還是a,但是指向的內存地址已經變了

>>> a=2 >>> id(a) 140406287260016 >>> a=a+2 >>> a 4 >>> id(a) 140406287259968

所以對于tuple對象(不可變對象),也是可以這樣操作的

>>> tuple1=(1,2) >>> id(tuple1) 4521580448 >>> tuple1=tuple1+(3,) >>> tuple1 (1, 2, 3) >>> id(tuple1) 4521658880

2、a+=2對于有些對象的操作是表示原來的對象,對有些對象的操作是生成了一個新對象

不可變對象tuple1,操作完后,內存地址已經發生變化,生成一個新的對象
>>> tuple1=(1,2) >>> type(tuple1) <type 'tuple'> >>> tuple1+=(3,) >>> id(tuple1) 4521658880 >>> tuple1+=(4,5) >>> id(tuple1) 4520649072

而list對象,可變對象,+=操作、append操作、extend操作,都是在原對象上操作

>>> list1=[1,2] >>> id(list1) 4521614656 >>> list1+=[3] >>> id(list1) 4521614656 >>> list1.append(4) >>> id(list1) 4521614656 >>> list1.extend(5) Traceback (most recent call last):File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable >>> list1.extend([5]) >>> id(list1) 4521614656 >>>

3、

x = [1,2,3] print "before func(), global! x = ",x,"id(x) = ",id(x) def func(): global x print "in func(), local! original x = ",x,"id(x) = ",id(x) x = x + [1] print "in func(), local! now x = ",x,"id(x) = ",id(x) func() print "after func(), global! x = ",x,"id(x) = ",id(x) 結果: [python] view plain copy before func(), global! x = [1, 2, 3] id(x) = 47781768 in func(), local! original x = [1, 2, 3] id(x) = 47781768 in func(), local! now x = [1, 2, 3, 1] id(x) = 47795720 after func(), global! x = [1, 2, 3, 1] id(x) = 47795720

global就保證了,即使我的變量x在函數中指向對象變了,外部的x也會指向新的對象

?

x = [1,2,3] print "before func(), global! x = ",x,"id(x) = ",id(x) def func(x): print "in func(), local! original x = ",x,"id(x) = ",id(x) x = x + [1] print "in func(), local! now x = ",x,"id(x) = ",id(x) func(x) print "after func(), global! x = ",x,"id(x) = ",id(x) 結果: before func(), global! x = [1, 2, 3] id(x) = 46339976 in func(), local! original x = [1, 2, 3] id(x) = 46339976 in func(), local! now x = [1, 2, 3, 1] id(x) = 46390664 after func(), global! x = [1, 2, 3] id(x) = 46339976

x = x + [1],是新建了一個對象,id(x) = ?46390664

?

利用id(x),查看下x += [1]對象是怎么變化的吧: x = [1,2,3] print "before func(), global! x = ",x,"id(x) = ",id(x) def func(x): print "in func(), local! original x = ",x,"id(x) = ",id(x) x += [1] print "in func(), local! now x = ",x,"id(x) = ",id(x) func(x) print "after func(), global! x = ",x,"id(x) = ",id(x) 結果: before func(), global! x = [1, 2, 3] id(x) = 46536584 in func(), local! original x = [1, 2, 3] id(x) = 46536584 in func(), local! now x = [1, 2, 3, 1] id(x) = 46536584 after func(), global! x = [1, 2, 3, 1] id(x) = 46536584
id(x)全程一樣,x += [1],python直接就在原對象上操作

?

?

參考:

1、http://blog.csdn.net/emaste_r/article/details/47395055

轉載于:https://www.cnblogs.com/shengulong/p/7906582.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的python中a=a+2与a+=2的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。