How do I determine the size of an object in Python?

前端 未结 13 1499
半阙折子戏
半阙折子戏 2020-11-21 21:53

I want to know how to get size of objects like a string, integer, etc. in Python.

Related question: How many bytes per element are there in a Python list (tuple)?

13条回答
  •  梦谈多话
    2020-11-21 22:35

    For numpy arrays, getsizeof doesn't work - for me it always returns 40 for some reason:

    from pylab import *
    from sys import getsizeof
    A = rand(10)
    B = rand(10000)
    

    Then (in ipython):

    In [64]: getsizeof(A)
    Out[64]: 40
    
    In [65]: getsizeof(B)
    Out[65]: 40
    

    Happily, though:

    In [66]: A.nbytes
    Out[66]: 80
    
    In [67]: B.nbytes
    Out[67]: 80000
    

提交回复
热议问题