亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python如何查看變量占用空間大小

Python如何查看變量占用空間大小

青春有我 2018-07-05 10:10:10
在c語言中提供了sizeof可以用來查看一個變量占用的空間大小,在python中可以使用sys模塊下的getsizeof方法來判斷變量占用的空間大小。其文檔解釋如下:[mw_shl_code=python,true]sys.getsizeof(object[, default]):[/mw_shl_code]Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.The default argument allows to define a value which will be returned if the object type does not provide means to retrieve the size and would cause a TypeError.getsizeof calls the object’s sizeof method and adds an additional garbage collector overhead if the object is managed by the garbage collector.使用示例:[mw_shl_code=python,true]import sysv = 1print sys.getsizeof(v)s = 'abc'print sys.getsizeof(s)[/mw_shl_code]
查看完整描述

1 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

sys.getsizeof(object[, default])

下面是我摘錄的,希望對你有用。

以字節(byte)為單位返回對象大小。 這個對象可以是任何類型的對象。 所以內置對象都能返回正確的結果 但不保證對第三方擴展有效,因為和具體實現相關。
getsizeof() 調用對象的 __sizeof__ 方法, 如果對象由垃圾收集器管理, 則會加上額外的垃圾收集器開銷。

查看完整回答
反對 回復 2018-07-28
  • 1 回答
  • 0 關注
  • 2969 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號