-7
>>> import sys
>>> print(sys.getsizeof(int()))
12
>>> print(sys.getsizeof(str()))
25
>>> mylist = [1,2,3,4,5,'ab']
>>> print(id(mylist))
50204144
>>> print(id(mylist[0]))
1849873456
>>> print(id(mylist[1]))
1849873472
>>> print(id(mylist[2]))
1849873488
>>> print(id(mylist[3]))
1849873504
>>> print(id(mylist[4]))
1849873520
>>> print(id(mylist[5]))
50209152

I don't know why the difference is 16:

64-bit operating system

L3viathan
  • 26,748
  • 2
  • 58
  • 81
Dong
  • 1
  • 1
  • 3
    Please do NOT post images of your code. Instead, post directly your code in code format (Ctr+K). – Fukiyel Mar 15 '19 at 10:21
  • output is 12 25 50204144 1849873456 1849873472 1849873488 1849873504 1849873520 50209152 – Dong Mar 15 '19 at 10:22
  • For the love of programming edit your post and put all of this inside it. – Fukiyel Mar 15 '19 at 10:23
  • 1
    This is an implementation detail of CPython. – L3viathan Mar 15 '19 at 10:23
  • Please update your post to enter the code instead of an image – Julien Mar 15 '19 at 10:24
  • 1
    it would be better if you put your code here and not pasting the screenshot. Describe what exactly you wanted and what you are getting. You can refer to this https://stackoverflow.com/help/how-to-ask – arctic_Oak Mar 15 '19 at 10:26
  • 2
    Why do you care about the distance anyway? It's an implementation detail, completely unspecified and arbitrary. The difference is what it is because, well, that's how CPython was written. – Aran-Fey Mar 15 '19 at 10:26
  • help(id) Help on built-in function id in module builtins: id(obj, /) Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (CPython uses the object's memory address.) – Dong Mar 15 '19 at 10:38
  • Thank you. I just used Print (help (id) – Dong Mar 15 '19 at 10:40

2 Answers2

1

Because they are int inside a list the location in memory of a integer goes 16 to 16 bits I truly recommend you to see this post: What is the id( ) function used for?

0

It looks like your question is: If sys.getsizeof(int()) is 12, then why are some of the the id() values 16 bytes apart instead of 12?

And it looks like you are expecting newly allocated ints to be 12 bytes away from one another because an int takes 12 bytes of storage.

If you are expecting that, it is because you are expecting a Python list to be like a C array, a chunk of contiguous memory where an array of five 8-byte objects takes exactly 40 bytes. But Python lists are not arrays, and list elements are not necessarily allocated in ascending memory order (let alone packed together). And so you can't expect the values of id() to be predictable from the amount of memory the object takes up.

By all means learn about the way Python's data structures are really allocated, if that really interests you. But it is a topic so advanced that few outside the CPython core team ever need to think about it. The rest of us are just content that it works. Which is why you are getting comments like it's an implementation detail and why do you care?

It is important to know how a C array is allocated because in C you manipulate memory pointers directly and getting that wrong can be catastrophic. But Python takes care of memory allocation for you, so knowing all the details of how it works is unlikely to make you a better Python programmer.

BoarGules
  • 16,440
  • 2
  • 27
  • 44