-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?

3
  • 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) Commented Oct 16, 2019 at 22:29
  • Check this answer Commented Oct 16, 2019 at 22:30
  • 1
    int is of type <class 'type'> and so are your other examples which is why they are all the same size and 1 is of type <class 'int'> which is why it's sized 28. Commented Oct 16, 2019 at 22:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.