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

Hugo Arts hugo.yoshi at gmail.com
Tue Jul 23 09:36:56 CEST 2013


On Tue, Jul 23, 2013 at 9:09 AM, Jim Mooney <cybervigilante at gmail.com>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? 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?:
>
>
In Python, the id is just a number that identifies the object. You can't
count on it to represent object size, or anything else for that matter. The
only requirement for python implementations is that different objects can
not have the same id.

The CPython implementation uses the memory address of the object as its id
because it's simple and satisfies the above requirement. Still, i'd say
it's very unsafe to try to infer object sizes from this. It depends on how
memory addresses work on your machine, what type of memory allocator is
used and how much overhead it has, and many more factors. Furthermore,
other python implementations (I believe Jython and IronPython among them)
do not even use memory addresses as id numbers, but simply assign
consecutive integers to each created object.

In general, relying on the id number to tell you anything other than
whether two variables point to the same object is highly unreliable at best
and impossible in the general case.

HTH,
Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130723/6ebc41b7/attachment.html>


More information about the Tutor mailing list