Playing with the python interpreter, I found the __sizeof__ method. The Docstrings say that this method returns the size, in bytes, of a given object.
Then, I asked myself: "What could be the size of an int
? 16, 32?", but, surprisingly, it's 28... Similarly, a float takes 4 more octets in memory. Plus, the size of an empty string, in memory, is 49 bytes. Finally, I was surprised to see that empty list, tuple, dict, an set take 40, 24, 264 and 200 bytes in memory, while if we add only one int
to each of these types, the size goes 8 bytes up for list and tuple, and doesn't change for dict and set... Finally, if we create simple classes, even if we add to them new attributs and methods, their size stayes 32 bytes, but simple functions take 112 bytes...
Why does theses types take this strange amount of memory ? Why adding a new int to differents containers doesn't take the same place in memory, while adding a new attribut to a class doesn't modify it's size ? Why does functions take that much place in memory compared to classical objects ?