[Tutor] object size in python is in what units?

Steven D'Aprano steve at pearwood.info
Tue Jul 23 09:40:54 CEST 2013


On 23/07/13 17:09, Jim Mooney wrote:
> I've noticed that when I create a number of objects from a class, one after
> another, they're at different IDs, but the IDs all appear to be
> equidistant, so that they all appear to have the same size. But what does
> that size represent? is it bytes?

No no no, a thousand times no!!! IDs are just numeric IDs, that is all, like your social security number or driver's licence number. Don't think of them as having any meaning at all, except that they are unique during the lifetime of the object.

Some Python implementations never reuse IDs, and they are numbered consecutively. IronPython numbers them from 40 (if memory serves me right), so you get 40, 41, 42, 43, ... while Jython numbers them consecutively from 1, 2, 3, ... PyPy does something similar.

CPython does reuse IDs, and uses the memory address of the base of the object, as reported by the operating system, which leads to annoying people saying that id() returns the address of the object. This is WRONG. It does not. id() returns an arbitrary ID number.


>ie - if I run this over and over I get
> different id numbers but they are always 40 apart. Or is that distance
> machine-specific as to units?:

Yes. It depends on whether CPython gets a chance to reuse the same memory address or not, whether you have a 32-bit or 64-bit Python compiler, whether any other objects are created in between each one, and many other factors.



-- 
Steven


More information about the Tutor mailing list