views:

6072

answers:

6

In C, we can find the size of an int, char, etc. I want to know how to get size of objects like a string, integer, etc. in Python.

Related question: How many bytes per element are there in a Python list (tuple)?

I am using an XML file which contains size fields that specify the size of value. I must parse this XML and do my coding. When I want to change the value of a particular field, I will check the size field of that value. Here I want to compare whether the new value that I'm gong to enter is of the same size as in XML. I need to check the size of new value. In case of a string I can say its the length. But in case of int, float, etc. I am confused.

A: 

Besides int and float, nothing is really fixed in size (And int is now long in Python 3, so so much for that). So you'd have to make a function sizeof function that specially works on the object based on type() or something. Either way, sizeof messes seem unpythonic

Demur Rumed
Even ints aren't fixed since they automatically spill over to long ints when they don't fit.
Cristian
-1: this function already exists.
nosklo
You are correct Cristian
+3  A: 

First: an answer.

import sys

try: print sys.getsizeof(object)
except AttributeError:
    print "sys.getsizeof exists in Python ≥2.6"

Discussion:
In Python, you cannot ever access "direct" memory addresses. Why, then, would you need or want to know how many such addresses are occupied by a given object?? It's a question that's entirely inappropriate at that level of abstraction. When you're painting your house, you don't ask what frequencies of light are absorbed or reflected by each of the constituent atoms within the paint, you just ask what color it is -- the details of the physical characteristics that create that color are beside the point. Similarly, the number of bytes of memory that a given Python object occupies is beside the point.

So, why are you trying to use Python to write C code? :)

Jeff Shannon
Your answer was incorrectly voted up since it was generally useful, but not an actual answer to the question. So I added an answer part first. As it was, it should be a comment to the question.
ΤΖΩΤΖΙΟΥ
The discussion part not justified. If you run into memory problems it can be very relevant to know where the memory goes. For example large scale scientific calculations using numpy easily run into this problem. Just because *you* can't think of a use case does not mean there isn't one!
nikow
+14  A: 

Not sure why you need it, knowing size is almost useless. But why not - Just use the sys.getsizeof function defined in the sys module.

sys.getsizeof(object[, default]):

Return the size of an object in bytes. The object can be any type of object. All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific.

The default argument allows to define a value which will be returned if the object type does not provide means to retrieve the size and would cause a TypeError.

getsizeof calls the object’s __sizeof__ method and adds an additional garbage collector overhead if the object is managed by the garbage collector.

Usage example, in python 3.0:

>>> import sys
>>> x = 2
>>> sys.getsizeof(x)
14
>>> sys.getsizeof(sys.getsizeof)
32
>>> sys.getsizeof('this')
38
>>> sys.getsizeof('this also')
48

If you are in python < 2.6 and don't have sys.getsizeof you can use this extensive module instead. Never used it though.

nosklo
how to do in Python 2.5.2 ?
@rejinacm: I've added info on how to do it on <2.6
nosklo
As a special case, to find out the largest integer supported by the integer type, use `sys.maxint`.
Goose Bumper
+1  A: 

It might be useful to see why your process eats up memory...

rapto
A: 

This can be more complicated than it looks depending on how you want to count things. For instance, if you have a list of ints, do you want the size of the list containing the references to the ints? (ie. list only, not what is contained in it), or do you want to include the actual data pointed to, in which case you need to deal with duplicate references, and how to prevent double-counting when two objects contain references to the same object.

You may want to take a look at one of the python memory profilers, such as pysizer to see if they meet your needs.

Brian
+1  A: 

For numpy arrays, getsizeof doesn't work - for me it always returns 40 for some reason:

from pylab import *
from sys import getsizeof
A = rand(10)
B = rand(10000)

Then (in ipython):

In [64]: getsizeof(A)
Out[64]: 40

In [65]: getsizeof(B)
Out[65]: 40

Happily, though:

In [66]: A.nbytes
Out[66]: 80

In [67]: B.nbytes
Out[67]: 80000
Mike Dewar