getting the size of an object
7stud
bbxx789_05ss at yahoo.com
Mon Jun 18 19:00:06 EDT 2007
On Jun 18, 10:07 am, "filox" <filox_realmmakni... at yahoo.com> wrote:
> is there a way to find out the size of an object in Python? e.g., how could
> i get the size of a list or a tuple?
>
> --
> You're never too young to have a Vietnam flashback
You can use the struct module to find the size in bytes:
import struct
mylist = [10, 3.7, "hello"]
int_count = 0
float_count = 0
char_count = 0
for elmt in mylist:
if type(elmt) == int:
int_count += 1
elif type(elmt) == float:
float_count += 1
elif type(elmt) == str:
char_count += len(elmt)
format_string = "%di%dd%dc" % (int_count, float_count, char_count)
list_size_in_bytes = struct.calcsize(format_string)
print list_size_in_bytes
--output:--
17
More information about the Python-list
mailing list