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

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

    You can make use of getSizeof() as mentioned below to determine the size of an object

    import sys
    str1 = "one"
    int_element=5
    print("Memory size of '"+str1+"' = "+str(sys.getsizeof(str1))+ " bytes")
    print("Memory size of '"+ str(int_element)+"' = "+str(sys.getsizeof(int_element))+ " bytes")
    

提交回复
热议问题