python的引用計數分析(二)

來源:互聯網
上載者:User

標籤:www   引用   pytho   就會   span   .com   列表   col   一個   

python所有對象引用計數被減少1的情況:

一.對象的別名被賦予新的對象;

a = 23345455  # 增加了一個引用b = a  # 增加了一個引用print(sys.getrefcount(a))b = 1.4  # 減少了一個23345455整數的引用print(sys.getrefcount(a))
結果:3;2
二.對象的別名被顯式銷毀;
a = 23345455  # 增加了一個引用b = a  # 增加了一個引用list = [a, b]  # 增加了2個引用del aprint(sys.getrefcount(b))
結果:4

說明:直接使用del關鍵字或者del()函數;注意:上述代碼手動銷毀的是被賦值引用的a,但是在列表裡的a不會被銷毀。

三.一個對象離開它的範圍;

a = 23345455  # 增加了一個引用b = a  # 增加了一個引用print(sys.getrefcount(a))  # 執行完畢後引用銷毀print(sys.getrefcount(a))

結果:3;3

說明:a作為參數傳遞到sys.getrefcount(a)函數中,只在函數中起作用,一旦執行完畢就會銷毀。

四.對象所在的容器被銷毀,或從容器中刪除對象;

# 增加了一個引用a = 23345455# 增加了一個引用b = alist = [a, b]  # 增加了2個引用del listprint(sys.getrefcount(b))

結果:3

所有的引用計數加一的情況: python的引用計數分析(一)

 

python的引用計數分析(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.