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

前端 未结 13 1460
半阙折子戏
半阙折子戏 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:45

    This can be more complicated than it looks depending on how you want to count things. For instance, if you have a list of ints, do you want the size of the list containing the references to the ints? (ie. list only, not what is contained in it), or do you want to include the actual data pointed to, in which case you need to deal with duplicate references, and how to prevent double-counting when two objects contain references to the same object.

    You may want to take a look at one of the python memory profilers, such as pysizer to see if they meet your needs.

提交回复
热议问题