[Python-Dev] PEP 42: sizeof(obj) builtin
M.-A. Lemburg
mal@lemburg.com
Fri, 31 Jan 2003 10:11:35 +0100
Tim Peters wrote:
> [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.
Some do; I wrote this for cache management to at least
have a hint at the memory size being used by the objects in
the cache.
>>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.
That's what I always telling people too ;-) sizeof() in
mxTools is only way to get at the tp_basicsize value, nothing
more. The rest can easily be done in Python itself.
--
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting: http://www.egenix.com/
Python Software: http://www.egenix.com/files/python/