How do I determine the size of an object in Python? -
in c, can find size of int
, char
, etc. want know how size of objects string, integer, etc. in python.
related question: how many bytes per element there in python list (tuple)?
i using xml file contains size fields specify size of value. must parse xml , coding. when want change value of particular field, check size field of value. here want compare whether new value i'm gong enter of same size in xml. need check size of new value. in case of string can length. in case of int, float, etc. confused.
just use sys.getsizeof function defined in sys
module.
sys.getsizeof(object[, default])
:return size of object in bytes. object can type of object. built-in objects return correct results, not have hold true third-party extensions implementation specific.
the
default
argument allows define value returned if object type not provide means retrieve size , causetypeerror
.
getsizeof
calls object’s__sizeof__
method , adds additional garbage collector overhead if object managed 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 in python < 2.6 , don't have sys.getsizeof
can use this extensive module instead. never used though.
Comments
Post a Comment