[Python-Dev] PEP 42: sizeof(obj) builtin

Tim Peters tim.one@comcast.net
Thu, 30 Jan 2003 19:44:59 -0500


[Raymond Hettinger]
> I'm about to start working on this one and wanted
> to check here first to make sure there is still a
> demand for it and to get ideas on the best implementation
> strategy.

Marc-Andre implemented it for mxTools:

    http://www.lemburg.com/files/python/mxTools.html

    sizeof(object)

    Returns the number of bytes allocated for the given Python object.
    Additional space allocated by the object and stored in pointers is not
    taken into account (though the pointer itself is). If the object
    defines tp_itemsize in its type object then it is assumed to be a
    variable size object and the size is adjusted accordingly.

I don't know whether anyone finds it useful in real life; maybe MAL has an
idea about that.


> I'm thinking of summing all of the tp_basicsize slots
> while recursing through tp_traverse.

So you'd add in the size of 0.0 a million times when traversing

    [0.0] * 1000000

?  That wouldn't be useful.  Keeping a memo to avoid double-counting might
be useful, but then it also gets more complicated, and so much better to
write it in Python building on something straighforward like mxTool's
version.