Getting different sizes of an object with different functions

Why is this piece of code giving different sizes in bytes with two different functions. I am using 32-bit version of python 2.7.3

1) with dictionaries: -

from sys import getsizeof l = range(20) d = {k:v for k,v in enumerate(l)} #creating a dict d.__sizeof__() #gives size in bytes 508 #size of dictionary 'd' in bytes getsizeof(d) 524 #size of same dictionary 'd' in bytes (which is different then above) 

2) with the list: -

 from sys import getsizeof l = range(20) l.__sizeof__() 100 #size of list 'l' in bytes getsizeof(l) 116 #size of same list 'l' in bytes 

3) with a tuple: -

 from sys import getsizeof t = tuple(range(20)) t.__sizeof__() 92 #size of tuple 't' in bytes getsizeof(t) 108 #size of same tuple 't' in bytes 

Someone will tell me why this behavior when the documentation of both functions says that they return the size of the object in bytes.

+4
source share
1 answer

From sys docs :

getizeof () calls the sizeof method of the objects and adds the extra overhead of the garbage collector if the object is managed by the garbage collector.

I guess this explains the discrepancy.

+4
source

Source: https://habr.com/ru/post/1496017/


All Articles