-1

Why does the following produce such different results?

>>> sys.getsizeof(int) # same as sys.getsizeof(object), sys.getsizeof(type)
400

>>> sys.getsizeof(1)
28

Is the actual size of the item 1 equal to the size of the object (400) + the size of the actual integer value (28) = 428, or how exactly does the integer/object creation work here?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 6
    Python **is not** C. `sys.getsizeof(int)` returns the size of the object referenced by `int`, which in this case, is the class-object for the built-in type `int`. That is not the same as the size of any of its instances. Note, integer objects *can have different sizes*. So compare `sys.getsizeof(0), sys.getsizeof(1), sys.getsizeof(10000000000)` – juanpa.arrivillaga Oct 16 '19 at 22:29
  • Check [this answer](https://stackoverflow.com/a/30316760/10366273) – MyNameIsCaleb Oct 16 '19 at 22:30
  • 1
    `int` is of type `` and so are your other examples which is why they are all the same size and `1` is of type `` which is why it's sized 28. – MyNameIsCaleb Oct 16 '19 at 22:38

1 Answers1

3

These objects aren't the same type which is why they aren't the same size:

>>> type(int)
<class 'type'>
>>> sys.getsizeof(type)
416
>>> type(object)
<class 'type'>
>>> type(type)
<class 'type'>

>>> type(1)
<class 'int'>
>>> sys.getsizeof(1)
28

There is a good answer on the general sizes of most objects and how they scale here.

And for the record, <class 'type'> is a metaclass and there is a super long answer here on what those are and why.

MyNameIsCaleb
  • 4,409
  • 1
  • 13
  • 31