1

When I tried the sys.getsizeof:

>>> import sys
>>> sys.getsizeof(int)
400
>>> sys.getsizeof(2)
28

I am puzzled about the result:

2 is an instance of class int but have much less bytes than its inherited class.

>>> sys.getsizeof(list)
400
>>> sys.getsizeof(list("list"))
120

The same is list.

Does 2 not inherit from class int?

  • @Ayxan nope, because that was wrong af. I don't know why it does that. – SuperStew Dec 06 '18 at 15:42
  • Possible duplicate of ["sys.getsizeof(int)" returns an unreasonably large value?](https://stackoverflow.com/questions/10365624/sys-getsizeofint-returns-an-unreasonably-large-value) – Paritosh Singh Dec 06 '18 at 15:49

1 Answers1

1

int is a class, that means it is a type object:

>>> type(int)
<class 'type'>
>>> from sys import getsizeof
>>> getsizeof(int)
400

sys.getsizeof returns the size of that object, not its instance. Use () to create an instance of int and see its size

>>> getsizeof(int())
24
>>> getsizeof(int(2))
28

>>> getsizeof(0)
24
>>> getsizeof(2)
28

This doesn't just apply to the built-in types, same behavior can be observed using user-defined classes:

>>> class a:
...     pass
... 
>>> getsizeof(a)
1056
>>> getsizeof(a())
56
>>> obj = a()
>>> getsizeof(obj)
56
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93