Embedding Python: estimate size of dict/list

Jerry Hill malaclypse2 at gmail.com
Mon Mar 28 19:56:15 EDT 2011


On Mon, Mar 28, 2011 at 7:18 PM, Chris Angelico <rosuav at gmail.com> wrote:
> I have an application that embeds Python to allow third-party
> scripting. The Python code returns data to the application in the form
> of a list or dictionary, and I'd like to have a quick check on the
> size of the outputted object before my code goes too deep into
> processing it.

For python 2.6 and later, sys.getsizeof() will probably do what you
want.  It relies on objects implementing a __sizeof__() method, so
third-party objects may or may not support this, but since you're
looking at dicts and lists, you should be all set.

-- 
Jerry



More information about the Python-list mailing list