2
import sys
print(sys.getsizeof(4))
print(sys.getsizeof(int))
print(sys.getsizeof(int()))

When i try to run the above code it displays the following output

28
400
24

But since all the given thing inside the parenthesis are int, so how it is giving different output. Can someone explain??

glibdud
  • 7,550
  • 4
  • 27
  • 37

1 Answers1

2

The first two are different as evident from doing:

>>> type(4)
<class 'int'>
>>> type(int)
<class 'type'>

The 3d differs from the first due to optimizing the integer space - the more bits you need, the more space Python will require in jumps of 4 - 0 is minimal, and you go up from there:

>>> getsizeof(0)
24
>>> getsizeof(32984732)
28
>>> getsizeof(3298473232432432432)
36

actually even 1 requires more space than 0 since that's the next size step.

kabanus
  • 24,623
  • 6
  • 41
  • 74