F
filox
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?
i get the size of a list or a tuple?
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?
Brett Hoerner said:"Size" can mean a lot of things,
len(my_list)
len(my_tuple)
Although I have the feeling you mean "how many bytes does this object
take in memory" - and I believe the short answer is no.
is there a long answer? what i want is to find out the number of bytes the
object takes up in memory (during runtime). since python has a lot of
introspection mechanisms i thought that should be no problem...
filox said:is there a long answer? what i want is to find out the number of bytes the
object takes up in memory (during runtime). since python has a lot of
introspection mechanisms i thought that should be no problem...
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?
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 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
is there a long answer? what i want is to find out the number of bytes
the
object takes up in memory (during runtime). since python has a lot of
introspection mechanisms i thought that should be no problem...
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?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.