The sys.getsizeof()
function returns the size of an object in bytes.
To use it, first import the sys
module:
main.py11 chars2 lines
Then, you can use the getsizeof()
function to determine the size of an object:
main.py71 chars3 lines
This will print out the size in bytes of the my_list
object, which is a list containing five integers. Note that the actual size might be larger than what's printed, because Python reserves some memory for its internal use.
You can use this function to determine the size of other objects as well, such as dictionaries:
main.py80 chars3 lines
Again, the actual size might be larger than what's printed.
gistlibby LogSnag