自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

內(nèi)存中的Python:Python引用計數(shù)指南

開發(fā) 后端
本文將會為你介紹Python引用計數(shù),演示中使用可變列表對象,不過本文不介紹C語言實現(xiàn)細(xì)節(jié)。

本文轉(zhuǎn)載自公眾號“讀芯術(shù)”(ID:AI_Discovery)

本文將會為你介紹Python引用計數(shù),演示中使用可變列表對象,不過本文不介紹C語言實現(xiàn)細(xì)節(jié)。

[[328257]]

需要注意的是,代碼段的輸出在硬件上可能有所不同。

變量是內(nèi)存引用

Python中的變量是內(nèi)存引用。如果輸入x = [1,2]時會發(fā)生什么?[1,2]是對象。

回想一下,一切都是Python中的對象。[1,2]將在內(nèi)存中創(chuàng)建。x是[1,2]對象的內(nèi)存引用。

來看看下面的例子。可以找到x所引用的內(nèi)存地址。請務(wù)必只使用id(x),它會以10為基數(shù),而十六進制函數(shù)會將其轉(zhuǎn)換為十六進制。

 

  1. x = [1, 2] 
  2.    print(hex(id(x)))  # output: 0x32ebea8 

 

內(nèi)存中的Python:Python引用計數(shù)指南

 

 

引用計數(shù)

現(xiàn)在已經(jīng)在內(nèi)存中創(chuàng)建了一個list對象,而且x對該對象進行了引用。那么y=[1,2]和y=x有什么區(qū)別?

當(dāng)輸入y=[1,2]時,它將在內(nèi)存中創(chuàng)建一個新的list對象,并且y將引用它。

 

  1. x = [1, 2] 
  2.    y = [1, 2] 
  3.    print(hex(id(x)))  # output: 0x101bea8 
  4.    print(hex(id(y)))  # output: 0x31a5528 

而當(dāng)輸入y=x時,等同于告訴Python希望y 變量引用x變量引用的內(nèi)容。因為變量是內(nèi)存引用的。

可以確認(rèn)x和y引用同一個對象。

 

  1. x = [1, 2] 
  2.    y = x 
  3.    print(hex(id(x)))  # output: 0x74bea8 
  4.    print(hex(id(y)))  # output: 0x74bea8 

 

內(nèi)存中的Python:Python引用計數(shù)指南

 

 

引用計數(shù)的數(shù)目

接下來的問題是,有多少變量引用同一個對象?

錯誤的用法:

我看到有些人在使用sys.getrefcount(var)時不知道如何傳遞var,而是向?qū)ο筇砑右?。一起看看下面的例子?/p>

輸出3,而期望的卻是2(x andy)。發(fā)生這種情況是因為將x傳遞給getrefcount函數(shù)時又添加了一個引用。

 

  1. from sys import getrefcount 
  2.           x = [1, 2] 
  3.           y = x 
  4.           print(hex(id(x)))  # output: 0xb65748 
  5.           print(hex(id(y)))  # output: 0xb65748 
  6.           print(getrefcount(x))  # output: 3 

更好的用法:

可以使用內(nèi)置的ctypes模塊來找到預(yù)期的結(jié)果。必須將x的id傳遞給from_address函數(shù)。

 

  1. from ctypes import c_long 
  2.       x = [1, 2] 
  3.       y = x 
  4.       print(hex(id(x)))  # output: 0x3395748 
  5.       print(hex(id(y)))  # output: 0x3395748 
  6.       print(c_long.from_address(id(x)).value)  # output: 2 

概言之,錯誤的用法是傳遞變量,而更好的用法則是傳遞變量的id,這意味著只傳遞基數(shù)為10的數(shù)字,而不是變量。

當(dāng)對象消失時

當(dāng)沒有變量引用對象時會發(fā)生什么?

對象將從內(nèi)存中刪除,因為沒有引用該對象的內(nèi)容。不過也有例外:如果有循環(huán)引用,garbage collector 將開始奏效。

為什么使用可變對象

不可變對象由于性能原因,結(jié)果可能與預(yù)期不同。查看下面的例子,觀察輸出是如何變化的。

 

  1. import sys 
  2.       import ctypes 
  3.              """Some Mutable Objects """ 
  4.       a =list() 
  5.       b =set() 
  6.       c =dict() 
  7.       d =bytearray() 
  8.       """ Some ImmutableObjects """ 
  9.       e =tuple() 
  10.       f =int() 
  11.       g =str() 
  12.       print(sys.getrefcount(a),ctypes.c_long.from_address(id(a)).value)  # output: 2 1 
  13.       print(sys.getrefcount(b),ctypes.c_long.from_address(id(b)).value)  # output: 2 1 
  14.       print(sys.getrefcount(c),ctypes.c_long.from_address(id(c)).value)  # output: 2 1 
  15.       print(sys.getrefcount(d),ctypes.c_long.from_address(id(d)).value)  # output: 2 1 
  16.       print(sys.getrefcount(e),ctypes.c_long.from_address(id(e)).value)  # output: 1298 1297 
  17.       print(sys.getrefcount(f),ctypes.c_long.from_address(id(f)).value)  # output: 209 208 
  18.       print(sys.getrefcount(g),ctypes.c_long.from_address(id(g)).value)  # output: 59 58 

 

文中所談及的一切都對CPython有效。希望對你有幫助。

 

責(zé)任編輯:華軒 來源: 讀芯術(shù)
相關(guān)推薦

2020-11-10 08:45:35

Python

2023-03-26 22:48:46

Python引用計數(shù)內(nèi)存

2017-10-12 12:41:11

PHP圾回收機制變量容器

2010-03-29 09:11:02

Python引用計數(shù)

2015-02-01 10:38:47

Linus并行計算

2010-08-19 09:24:41

iPhone

2020-02-09 17:23:17

Python數(shù)據(jù)字典

2021-05-27 21:47:12

Python垃圾回收

2013-08-21 10:53:46

iOS定義區(qū)別

2010-03-15 12:36:26

Python列表

2024-05-13 08:35:27

PyObjectPython對象

2023-10-26 11:19:21

指針Go

2024-07-18 09:07:04

Python窗口操作

2021-08-10 07:27:42

Python引用計數(shù)法

2021-06-28 08:00:00

Python開發(fā)編程語言

2017-11-15 19:30:08

Python內(nèi)存泄露循環(huán)引用

2021-12-09 15:45:09

Python弱引用代碼

2017-02-09 21:24:22

iOS內(nèi)存管理

2017-07-18 11:12:39

環(huán)境設(shè)置內(nèi)存分析Python

2011-07-07 09:54:01

Cocoa Core Foundation
點贊
收藏

51CTO技術(shù)棧公眾號